案例: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 8 9 1064 6511 12 My JSP 'index.jsp' starting page 13 14 15 16 17 18 21 22 23 24用户登录
25
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
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 8 9 1011 12 My JSP 'dologin.jsp' starting page 13 14 15 16 17 18 19 22 23 24 25 26登录成功
27
28 29 30 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 else50 { //如果用户没有勾选记住用户名、密码的复选框,则把已经保存的原来的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 查看用户信息67 68 69
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 8 9 1011 12 My JSP 'users.jsp' starting page 13 14 15 16 17 18 19 22 23 24 25 26用户信息
27
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 49 50 51 用户名:<%=username %>52 密码:<%=password %>53 54