cookie之三天免登录代码
LoginCookie.java
1 package com.bjsxt.cookie; 2 3 import java.io.IOException; 4 import java.net.URLDecoder; 5 import java.net.URLEncoder; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.Cookie; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 /** 15 * Servlet implementation class LoginCookie 16 */ 17 @WebServlet("/LoginCookie") 18 public class LoginCookie extends HttpServlet { 19 20 @Override 21 protected void service(HttpServletRequest req, HttpServletResponse resp) 22 throws ServletException, IOException { 23 System.out.println("LoginCookie.service()"); 24 25 //处理中文乱码 26 req.setCharacterEncoding("utf-8"); 27 resp.setCharacterEncoding("utf-8"); 28 resp.setContentType("text/html;charset=utf-8"); 29 30 //获取请求参数 31 String name=req.getParameter("uname"); 32 String pwd=req.getParameter("pwd"); 33 String check=req.getParameter("che"); 34 35 //处理业务 36 //1:判断是否选择三天免登录 37 if(check!=null&&"yes".equals(check)){ 38 //判断用户名和密码是否为空 39 if((name!=null&&!"".equals(name))&&(pwd!=null&&!"".equals(pwd))){ 40 //设置cookie,保存在客户端的硬盘中 41 42 Cookie cookiename=new Cookie("cookiename",URLEncoder.encode(name,"utf-8")); 43 Cookie cookiepwd=new Cookie("cookiename",URLEncoder.encode(pwd,"utf-8")); 44 System.out.println("LoginCookie.service(存储cookie)"); 45 //设置cookie的有效期 46 cookiename.setMaxAge(3*24*3600); 47 cookiepwd.setMaxAge(3*24*3600); 48 49 //设置访问路径 50 cookiename.setPath(req.getContextPath()+"/LoginCookie"); 51 cookiepwd.setPath(req.getContextPath()+"/LoginCookie"); 52 53 //发送给浏览器客户端 54 resp.addCookie(cookiename); 55 resp.addCookie(cookiepwd); 56 } 57 58 //如果用户名和密码为空,从客户端硬盘中读取cookie 59 if((name==null||"".equals(name))||(pwd==null||"".equals(pwd))){ 60 //读取cookie 61 Cookie[] cookies=req.getCookies(); 62 //过滤出用户名和密码 63 if(cookies!=null&&cookies.length>0){ 64 for(int i=0;i<cookies.length;i++){ 65 //取出用户名 66 if("cookiename".equals(cookies[i].getName())){ 67 //pwd=cookies[i].getValue(); 68 name=URLDecoder.decode(cookies[i].getValue(),"utf-8"); 69 } 70 //取出密码 71 if("cookiepwd".equals(cookies[i].getName())){ 72 pwd=URLDecoder.decode(cookies[i].getValue(),"utf-8"); 73 } 74 } 75 } 76 //打印英户名和密码 77 System.out.println("LoginCookie.service()name:"+name+"---pwd"+pwd); 78 } 79 //4校验用户名和密码 80 if("张三".equals(name)&&"abc".equals(pwd)){ 81 resp.getWriter().print("welcome"+name+"login success"); 82 }else{ 83 resp.getWriter().print("login false "+name+pwd); 84 } 85 } 86 } 87 }
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="LoginCookie" method="post"> 11 <table border="1" align="center"> 12 <tr> 13 <th>用户名:</th> 14 <td><input type="text" name="uname" value=""></td> 15 </tr> 16 <tr> 17 <th>密码</th> 18 <td><input type="password" name="pwd" value=""></td> 19 </tr> 20 <tr> 21 <th colspan="2"><input type="checkbox" name="che" value="yes">三天免登录</th> 22 </tr> 23 <tr> 24 <th colspan="2"><input type="submit" value="登录"></th> 25 </tr> 26 </table> 27 </form> 28 </body> 29 </html>
个人学习笔记,记录日常学习,便于查阅及加深,仅为方便个人使用。