Java WEB 用户登录+Cookie技术

Java WEB 用户登录+Cookie技术_第1张图片 Java WEB 用户登录+Cookie技术_第2张图片 Java WEB 用户登录+Cookie技术_第3张图片

 

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 
 8 
 9   
10     
11     
12     Cookie
13     
14     
15         
16     
17     
18     
21   
22   
23   
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           
45

用户登陆

46
47
48 49505152535455565758596061626364656667
用户名:
密码:
记住登陆信息
68
69
70 71

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 
 8 
 9   
10     
11     
12     My JSP 'dologin.jsp' starting page
13     
14     
15     
16         
17     
18     
19     
22 
23   
24   
25   
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      查看用户信息
59   
60 
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 
 8 
 9   
10     
11     
12     My JSP 'users.jsp' starting page
13     
14     
15     
16         
17     
18     
19     
22 
23   
24   
25   
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     
46

用户信息

47
48 用户名:<%=username %> 49
50 密码:<%=password %> 51
52 53 54
users.jsp

 

转载于:https://www.cnblogs.com/cityhuntshou/p/7440304.html

你可能感兴趣的:(java)