Java WEB 用戶登錄+Cookie技術


  

 

login.jsp................................用戶登錄頁面

dologin.jsp............................處理用戶登錄邏輯,驗證用戶登錄,保存用戶登錄狀態到Session

user.jsp.................................讀取Seesion狀態,顯示用戶信息

login.jsp代碼示例:

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>Cookie</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24   <%
25        String username = "";
26        String password = "";
27        Cookie[] cookies = request.getCookies();
28        if(cookies != null && cookies.length > 0)
29        {
30            for(Cookie c : cookies)
31            {
32                if(c.getName().equalsIgnoreCase("username"))
33                {
34                    username = c.getValue();
35                }
36                if(c.getName().equalsIgnoreCase("password"))
37                {
38                    password = c.getValue();
39                }
40            }
41        }
42        
43        %>
44           <center>
45               <h1>用戶登陸</h1>
46               <hr>
47               <form action="dologin.jsp" method="post" name="loginFrom">
48                   <table>
49                       <tr>
50                           <td>用戶名:</td>
51                           <td><input type="text" name="username" value="<%=username%>" /></td>
52                       </tr>
53                       
54                       <tr>
55                           <td>密碼:</td>
56                           <td><input type="password" name="password" value="<%=password %>" /></td>
57                       </tr>
58                       
59                       <tr>
60                           <td  colspan="2" align="center"><input type="checkbox" checked="checked" name="isUseCookie" />記住登陸信息</td>
61                       </tr>
62                       
63                       <tr>
64                           <td  colspan="2" align="center"><input type="submit" value="登陸" /></td>
65                       </tr>
66                                         
67                   </table>
68               </form>
69           </center>
70   </body>
71 </html>

dologin.jsp代碼:

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'dologin.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <%
27         //首先判斷用戶是否選擇了記住登陸狀態
28         String[] isUseCookie = request.getParameterValues("isUseCookie");
29         if(isUseCookie != null && isUseCookie.length > 0)
30         {
31             //把用戶名和密碼保存在Cookie對象里面
32             String username = request.getParameter("username");
33             String password = request.getParameter("password");
34             Cookie usernameCookie = new Cookie("username",username);
35             Cookie passwordCookie = new Cookie("password",password);
36             usernameCookie.setMaxAge(86400);
37             passwordCookie.setMaxAge(86400);
38             response.addCookie(usernameCookie);
39             response.addCookie(passwordCookie);
40         }
41         else 
42         {
43             //已保存Cookie設置失效
44             Cookie[] cookies = request.getCookies();
45             if(cookies != null && cookies.length > 0)
46             {
47                 for(Cookie c : cookies)
48                 {
49                     if(c.getName().equalsIgnoreCase("username") || c.getName().equalsIgnoreCase("password"))
50                     {
51                         c.setMaxAge(0); //設置Cookie失效
52                         response.addCookie(c); //重新保存Cookie
53                     }
54                 }
55             }
56         }
57      %>
58      <a href="users.jsp" target="_blank">查看用戶信息</a>
59   </body>
60 </html>
View Code

users.jsp代碼:

 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'users.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26    <%
27        String username = "";
28        String password = "";
29        Cookie[] cookies = request.getCookies();
30        if(cookies != null && cookies.length > 0)
31        {
32            for(Cookie c : cookies)
33            {
34                if(c.getName().equalsIgnoreCase("username"))
35                {
36                    username = c.getValue();
37                }
38                if(c.getName().equalsIgnoreCase("password"))
39                {
40                    password = c.getValue();
41                }
42            }
43        }
44     %>
45     <center>
46         <h1>用戶信息</h1>
47         <hr>
48          用戶名:<%=username %>
49          <br>
50          密碼:<%=password %>
51     </center>
52    
53   </body>
54 </html>
users.jsp

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM