JAVA遇见HTML——JSP篇(JSP状态管理)
案例:Cookie在登录中的应用
URL编码与解码的工具类解决中文乱码的问题,这个工具类在java.net.*包里
编码:URLEncoder.encode(String s,String enc)//s:对哪个字符串进行编码,enc:用的字符集(例:utf-8)
解码:URLDecoder.decode(String s,String enc)//s:对哪个字符串进行解码,enc:用哪个字符集解码(例:utf-8)
login.jsp
1 <%@ page language="java" import="java.util.*,java.net.*" 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 'index.jsp' starting page</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 <h1>用户登录</h1> 25 <hr> 26 <% 27 request.setCharacterEncoding("utf-8"); 28 String username=""; 29 String password = ""; 30 Cookie[] cookies = request.getCookies(); 31 if(cookies!=null&&cookies.length>0) 32 { 33 for(Cookie c:cookies) 34 { 35 if(c.getName().equals("username")) 36 { 37 username = URLDecoder.decode(c.getValue(),"utf-8"); 38 } 39 if(c.getName().equals("password")) 40 { 41 password = URLDecoder.decode(c.getValue(),"utf-8"); 42 } 43 } 44 } 45 %> 46 <form name="loginForm" action="dologin.jsp" method="post"> 47 <table> 48 <tr> 49 <td>用户名:</td> 50 <td><input type="text" name="username" value="<%=username %>"/></td> 51 </tr> 52 <tr> 53 <td>密码:</td> 54 <td><input type="password" name="password" value="<%=password %>" /></td> 55 </tr> 56 <tr> 57 <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天内记住我的登录状态</td> 58 </tr> 59 <tr> 60 <td colspan="2" align="center"><input type="submit" value="登录"/><input type="reset" value="取消"/></td> 61 </tr> 62 </table> 63 </form> 64 </body> 65 </html>
dologin.jsp
1 <%@ page language="java" import="java.util.*,java.net.*" 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 <h1>登录成功</h1> 27 <hr> 28 <br> 29 <br> 30 <br> 31 <% 32 request.setCharacterEncoding("utf-8"); 33 //首先判断用户是否选择了记住登录状态 34 String[] isUseCookies = request.getParameterValues("isUseCookie"); 35 if(isUseCookies!=null&&isUseCookies.length>0) 36 { 37 //把用户名和密码保存在Cookie对象里面。 38 String username = URLEncoder.encode(request.getParameter("username"),"utf-8"); 39 //使用URLEncoder解决无法在Cookie当中保存中文字符串问题 40 String password = URLEncoder.encode(request.getParameter("password"),"utf-8"); 41 42 Cookie usernameCookie = new Cookie("username",username); 43 Cookie passwordCookie = new Cookie("password",password); 44 usernameCookie.setMaxAge(864000); 45 passwordCookie.setMaxAge(864000);//设置最大生存期限为10天 46 response.addCookie(usernameCookie); 47 response.addCookie(passwordCookie); 48 } 49 else 50 {//如果用户没有勾选记住用户名、密码的复选框,则把已经保存的原来的cookie设置失效 51 Cookie[] cookies = request.getCookies(); 52 //曾经保存过用户名、密码 53 if(cookies!=null&&cookies.length>0) 54 { 55 for(Cookie c:cookies) 56 { 57 if(c.getName().equals("username")||c.getName().equals("password")) 58 { 59 c.setMaxAge(0); //设置Cookie失效 60 response.addCookie(c); //重新保存。 61 } 62 } 63 } 64 } 65 %> 66 <a href="users.jsp" target="_blank">查看用户信息</a> 67 68 </body> 69 </html>
user.jsp
1 <%@ page language="java" import="java.util.*,java.net.*" 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 <h1>用户信息</h1> 27 <hr> 28 <% 29 request.setCharacterEncoding("utf-8"); 30 String username=""; 31 String password = ""; 32 Cookie[] cookies = request.getCookies(); 33 if(cookies!=null&&cookies.length>0) 34 { 35 for(Cookie c:cookies) 36 { 37 if(c.getName().equals("username")) 38 { 39 username = URLDecoder.decode(c.getValue(),"utf-8"); 40 } 41 if(c.getName().equals("password")) 42 { 43 password = URLDecoder.decode(c.getValue(),"utf-8"); 44 } 45 } 46 } 47 %> 48 <BR> 49 <BR> 50 <BR> 51 用户名:<%=username %><br> 52 密码:<%=password %><br> 53 </body> 54 </html>