1. 程式人生 > >Session案例_登錄註銷

Session案例_登錄註銷

構造 網站 text tro ack 張無忌 port for 執行

技術分享

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
      <h1>我的網站</h1><hr>
      <%
          //獲取session中的登錄狀態
        String user = (String)session.getAttribute("
user");//這裏的 user是session的標記 %> <% if(user == null || "".equals(user)){//用戶沒有登錄 %> 歡迎光臨!遊客! <a href="${pageContext.request.contextPath }/loginout/login.jsp">登錄</a> <a href="#">註冊</a> <% }
else{//用戶登錄過 %> 歡迎回來!<%=user %>! <a href="${pageContext.request.contextPath }/servlet/LogoutServlet">註銷</a> <% } %> </body> </html>

login.jsp

<%@ page language="java" import="
java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <h1>我的網站-登錄</h1><hr> <form action="${pageContext.request.contextPath }/servlet/LoginServlet" method="POST"> 用戶名:<input type="text" name="username"/> 密碼:<input type="password" name="password"/> <input type="submit" value="登錄"/> </form> </body> </html>

LoginServlet.java

 1 package com.itheima.session.loginout;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class LoginServlet extends HttpServlet {
11 
12     public void doGet(HttpServletRequest request, HttpServletResponse response)
13             throws ServletException, IOException {
14         request.setCharacterEncoding("utf-8");//對於17,18行起作用!!!
15         response.setContentType("text/html;charset=utf-8");//對27行起作用!!!
16         //1.獲取用戶名密碼
17         String username = request.getParameter("username");
18         String password = request.getParameter("password");
19         //2.查詢數據庫檢查用戶名密碼
20         if(UserDao.valiNamePsw(username, password)){
21             //3.如果正確登錄後重定向到主頁
22             request.getSession().setAttribute("user", username);//設置session標記user
23             response.sendRedirect(request.getContextPath()+"/loginout/index.jsp");
24             return;
25         }else{
26             //4.如果錯誤提示
27             response.getWriter().write("用戶名密碼不正確!");
28         }
29     }
30 
31     public void doPost(HttpServletRequest request, HttpServletResponse response)
32             throws ServletException, IOException {
33         doGet(request, response);
34     }
35 
36 }

UserDao.java

 1 package com.itheima.session.loginout;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 public class UserDao {
 7     private UserDao() {//工具類的構造方法私有化
 8     }
 9     private static Map <String,String>map = new HashMap<String, String>();//因為驗證的信息是用戶名、密碼,可以用key、value
的形式,想到用map語法
10 static{//類加載,靜態代碼塊執行 11 map.put("張三豐", "111"); 12 map.put("張翠山", "999"); 13 map.put("張無忌", "888"); 14 map.put("趙敏", "777"); 15 } 16 public static boolean valiNamePsw(String username,String password){ 17 return map.containsKey(username) && map.get(username).equals(password); 18 } 19 }

LogoutServlet.java

 1 package com.itheima.session.loginout;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class LogoutServlet extends HttpServlet {
11 
12     public void doGet(HttpServletRequest request, HttpServletResponse response)
13             throws ServletException, IOException {
14         //1.殺死session
15         //request.getSession()底層的實現,如果發現沒有session立即創建
16         if(request.getSession(false)!=null //如果沒有對應的session,返回null,不會創建session
17                 && request.getSession().getAttribute("user")!=null){
18     //  request.getSession(false)!=null && request.getSession().getAttribute("user")!=null  表示有sesson,並且有登錄標記
19             request.getSession().invalidate();
20         }
21         //2.重定向到主頁
22         response.sendRedirect(request.getContextPath()+"/loginout/index.jsp");
23     }
24 
25     public void doPost(HttpServletRequest request, HttpServletResponse response)
26             throws ServletException, IOException {
27         doGet(request, response);
28     }
29 
30 }

Session案例_登錄註銷