1. 描述
從登錄頁面登錄後,如果登錄成功,那麼進入主頁;如果登錄失敗,那麼進入到一個失敗的頁面。
2. 分析
(1) 首先有一個登錄頁面login.html,提交登錄後交給後臺去處理,後臺接收到以後交給LoginServlet專門處理登
錄的邏輯。如果登錄失敗,就進入到一個失敗的頁面fail.html;如果登錄成功,就進入主頁,主頁用一個Servlet
去顯示,比如:主頁IndexServlet,因爲要顯示動態的數據。
(2) 要把LoginServlet中的用戶名傳遞給IndexServlet共享使用,就需要使用到域對象。Session域對象是合適
的,因爲每一個用戶登錄後,就會給每一個用戶分配唯一的session對象。比如:張三和李四分別用不同的瀏覽器
登錄網站,登錄時都要訪問LoginServlet。
① 當張三訪問LoginServlet的時候,就會創建一個session對象session1,在裏面存放數據,創建完對象之後,
Session的機制就會給session1分配一個ID,比如s001,這個ID生成後會返回給張三的瀏覽器,那麼張三就有了
Session的編號,當下次張三來拿數據的時候也可以拿到。
② 當李四訪問登錄LoginServlet的時候,如果李四沒有編號,也拿不到張三的數據,只會重新創建一個Session
對象Session2,並存放一些數據,分配編號S002,把這個編號貼給李四的瀏覽器。
③ 所以張三和李四下次訪問的,都是訪問自己特有的一個對象,這就實現了每個人都擁有一個特有的域對象。
3. 實現
(1) 處理登錄的邏輯
/*
* 處理登錄的邏輯
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// 1.接收參數
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
// 2.判斷邏輯
if ("eric".equals(userName) && "123456".equals(userPwd)) {
// 登錄成功
/*
* 分析: context域對象:不合適,可能會覆蓋數據。比如開始是張三,但李四登錄後,就會把張三的數據覆蓋掉。
* request域對象:不合適,整個網站必須得使用轉發技術來跳轉頁面,如果直接訪問主頁,那麼登錄信息就會消失。登錄之後就不能隨便訪問用戶主頁。也就是說如果要到首頁那麼每次都得登錄。
* session域對象:合適。
*/
/*
* 一、登錄成功後,把用戶數據保存session對象中
*/
// 1.創建session對象
HttpSession session = request.getSession();
// 2.把數據保存到session域中
session.setAttribute("loginName", userName);
// 3.跳轉到用戶主頁
response.sendRedirect(request.getContextPath() + "/IndexServlet");
} else {
// 登錄失敗
// 請求重定向
response.sendRedirect(request.getContextPath() + "/fail.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
(2) 用戶主頁的邏輯
/*
* 用戶主頁的邏輯
*/
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
String html = "";
/*
* 接收request域對象的數據
*/
/*
* 二、在用戶主頁,判斷session不爲空且存在指定的屬性才視爲登錄成功!才能訪問資源。 從session域中獲取會話數據
*/
// 1.得到session對象
HttpSession session = request.getSession(false);
if (session == null) {
// 沒有登錄成功,跳轉到登錄頁面
response.sendRedirect(request.getContextPath() + "/login.html");
return;
}
// 2.取出會話數據
String loginName = (String) session.getAttribute("loginName");
if (loginName == null) {
// 沒有登錄成功,跳轉到登錄頁面
response.sendRedirect(request.getContextPath() + "/login.html");
return;
}
html = "<html><body>歡迎回來," + loginName + ",<a href='" + request.getContextPath()
+ "/LogoutServlet'>安全退出</a></body></html>";
writer.write(html);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
(3) 安全退出邏輯
/*
* 安全退出邏輯
*/
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 三、安全退出: 不能註銷Session對象,如果銷燬,不僅僅註銷了登錄的狀態,而且還把保存的數據也丟失。
* 而應該刪除掉session對象中指定的loginName屬性值即可!
*/
// 1.得到session對象
HttpSession session = request.getSession(false);
if (session != null) {
// 2.刪除屬性
session.removeAttribute("loginName");
}
// 3.回來登錄頁面
response.sendRedirect(request.getContextPath() + "/login.html");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
(4) 登錄頁面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登錄頁面</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="/login/LoginServlet" method="post">
用戶名:<input type="text" name="userName"/>
<br/>
密碼:<input type="text" name="userPwd"/>
<br/>
<input type="checkbox" name="vehicle" value="Car" checked="checked" />
<input type="submit" value="登錄"/>
</form>
</body>
</html>
(5) 登錄失敗頁面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>信息提示頁面</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<font color='red' size='3'>親, 你的用戶名或密碼輸入有誤!請重新輸入!</font><br/>
<a href="/login/login.html">返回登錄頁面</a>
</body>
</html>
4. 結果
(1) 登錄失敗
(2) 登錄成功