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代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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>
users.jsp代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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>