セッションは、多くの場合、ユーザーのログイン情報を格納するために使用されます。次に、サーバは、それを達成する方法ですか?ここでは、私は単にそれをシミュレートします。
- ログインページを書いて最初のステップ:
<!DOCTYPE html>
<html>
<head>
<title>登陆页面</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/JavaWeb/LoginServlet" method="post">
用户名:<input type="text" name="userName"/>
<br/>
密码: <input type="text" name="userPwd"/>
<br/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>
- 次は、ので、ここで、あるため、サーバのリソース情報を取得するためには、要求を処理するために使用されるdoGetメソッドをがloginservletクラスを作成します。実際のプロセスは確かにすることができ、セッションの使用状況を理解することを目的とした単純化したデータベース内のユーザー情報を照会する必要があります。
package gz.itcast;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//接收参数
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
//判断登陆是否成功
if(userName.equals("刘小斌") && userPwd.equals("123456")) {
//登陆成功
//创建session对象
HttpSession session = request.getSession();
//把用户数据保存在session域对象中
session.setAttribute("loginName", userName);
//跳转到用户主页
response.sendRedirect(request.getContextPath()+"/IndexServlet");
} else {
//登陆失败,请求重定向
response.sendRedirect(request.getContextPath() + "/fail.html");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
- 正常に作成リクエストIndexServletクラス:
package gz.itcast;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class IndexServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
String html = "";
//得到session对象
HttpSession session = request.getSession(false);
if(session==null){
//没有登录成功,跳转到登录页面
response.sendRedirect(request.getContextPath()+"/login.html");
return;
}
//取出会话数据
String loginName = (String)session.getAttribute("loginName");
if(loginName==null){
//没有登录成功,跳转到登录页面
response.sendRedirect(request.getContextPath()+"/login.html");
return;
}
html = "<html><body>欢迎回来," +loginName+ "</a></body></html>";
writer.write(html);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
- 要求が失敗したために失敗ページが作成されました:
<!DOCTYPE html>
<html>
<head>
<title>信息提示页面</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<font color = red size = 3>用户名或密码输入错误!请重新输入。</font>
</br></br>
<a href="/JavaWeb/login.html">返回登陆页面,重新输入。</a>
</body>
</html>
プロジェクト階層:
さて、ここでTomcatサーバーを開くには、結果を見て:
-
ログイン画面のURLを入力します。
-
ログイン名やパスワードエラーを入力します。
-
エラーページに移動:
-
正しいユーザー名とパスワードを入力します。
-
ユーザーホームページにジャンプします:
注、その後、ユーザーのホーム・ページまたは他のいくつかの操作を更新し、ユーザー情報がまだあります。セッションが消えない限り、セッション・ドメイン・オブジェクトが表示されなくなります。
いくつかの問題が運転中に発生:変更後の直接見つけセッションクラス名が間違っは、web.xml構成、または404エラーに行く必要があり、十分ではありません。リダイレクトプロセスのURLが間違っている場合もちろん、それは500エラーになります。
注:元の特別転載の損失を防ぐためhttps://blog.csdn.net/liu_wenbin/article/details/59531184
以下は、償却ログは次のようになります。
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.getSession().removeAttribute("user");
out.write("注销成功");
response.setHeader("Refresh", "2;URL="+request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}