分析:1.先判断session
2.保存cookie
①用户名。密码。有效期。网站标识
②以上四个字符串 用 :分割开来 用MD5加密(是不可逆 的)
③用户名:有效期:MD5加密后的字符串
④response.andcookie(new cookie(“cookieName”,用户名:有效期:MD5加密后的字符串));
3.读取cookie
①根据cookieName取得值
②sqlit(:)
③分离获得 用户名 获得user对象
步骤:1、创建util包,再建cookieUtil类
2、cookieUtil类里面的内容
package cn.bdqn.demo.util;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.bdqn.demo.Esaybuy_user.entity.Esaybuy_user;
public class cookieUtil {
//cookieName
private final static String cookieName="demo";
//网站标识
private final static String cookiebiazhi="cn.bdqn.demo";
//有效期(两周)
private final static long cookieTime=60*60*24*7*2;
//保存cookie(在登录的Servlet中登录成功后调用)
public static void savecookie(HttpServletResponse response,Esaybuy_user user){
String username=user.getEu_user_name();
String password =user.getEu_password();
//用户名:密码:有效期:网站标识 用MD5加密
String md5String
=getMD5(username+":"+password+":"+cookieTime+":"+cookiebiazhi);
//将用户名、有效期、加密字符串用‘:’连接 保存在cookie中
String cookievalue=username+":"+cookieTime+":"+md5String;
Cookie coo=new Cookie(cookieName,cookievalue);
coo.setMaxAge(60*60*24*7*2);
response.addCookie(coo);
}
//用MD5进行加密 返回加密后的字符串
private static String getMD5(String value){
String re="";
try {
MessageDigest md= MessageDigest.getInstance("MD5");
md.update(value.getBytes());
re=md.digest().toString();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return re;
}
//读cookie(在登录的过滤器中 如果没有session再读 cookie)
public static void readcookie(ServletRequest request, ServletResponse response,
FilterChain chain) {
try {
PrintWriter out =response.getWriter();
String cookieValue="";
HttpServletRequest htrequest=(HttpServletRequest)request;
Cookie [] cookies=htrequest.getCookies();
for(Cookie cookie:cookies){
//根据cookieName取得值
if(cookieName.equals(cookie.getName())){
//判断该cookie的时间是否超过有效时间
if(cookie.getMaxAge()>60*60*24*7*2){
//setMaxAge(0)表示删除cookie
cookie.setMaxAge(0);
break;
}
cookieValue=cookie.getValue();
}
}
String [] cookievalueArray=cookieValue.split(":");
if(cookievalueArray.length==3){
String username=cookievalueArray[0];
String cookTime=cookievalueArray[1];
String mdsString=cookievalueArray[2];
//根据用户名得到用户实体类
Esaybuy_user user=new Esaybuy_user();
user=userService.logincook(username);
//得到第二次加密的字符串
String md5String=getMD5(user.getEu_user_name()+":"+user.getEu_password()+":"+cookTime+":"+cookiebiazhi);
if(mdsString.equals(md5String)){
chain.doFilter(request, response);
}else{ //如果以前加的密 与现在加的密不一样 说明数据库里的信息已将改变 将跳到登录界面
String url=htrequest.getContextPath()+"/login.jsp"; //获得当前项目的路径加上 后面的
request.getRequestDispatcher(url).forward(htrequest, response);
}
}else{
out.print("cookie读取失败");
String url=htrequest.getContextPath()+"/login.jsp"; //获得当前项目的路径加上 后面的
request.getRequestDispatcher(url).forward(htrequest, response);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}