cookie记住账户密码
1 //cookie保存路径 2 String autologin=request.getParameter("autologin"); 3 String path = request.getContextPath()+"/"; 4 if(autologin!=null&&autologin.equals("on")){ 5 Cookie cookie = new Cookie("user",username+"-"+password); 6 cookie.setMaxAge(60 * 60 * 24 * 14); 7 cookie.setPath(path); 8 response.addCookie(cookie); 9 }else{//清空cookie 10 Cookie[] cookies = request.getCookies(); 11 for(int i=0;i<cookies.length;i++){ 12 if(cookies[i].getName().equals("user")){ 13 //System.out.println(cookies[i].getName() + ":" + cookies[i].getValue()); 14 Cookie cookie = new Cookie(cookies[i].getName(), null); 15 cookie.setMaxAge(0); 16 cookie.setPath(path);//根据你创建cookie的路径进行填写 17 response.addCookie(cookie); 18 } 19 } 20 }
1 //页面取cookie 2 <% 3 String name=""; 4 String passward=""; 5 Cookie[] cookies=request.getCookies(); 6 if(cookies!=null){ 7 for(int i=0;i<cookies.length;i++){ 8 if(cookies[i].getName().equals("user")){ 9 name=cookies[i].getValue().split("-")[0]; 10 passward=cookies[i].getValue().split("-")[1]; 11 request.setAttribute("username",name); 12 request.setAttribute("password",passward); 13 } 14 } 15 } 16 %>