//保存cookie
Cookie cookieName = new Cookie("name", realUser.getName());
Cookie cookiePwd = new Cookie("pwd", realUser.getPwd());
Cookie cookieId = new Cookie("id", realUser.getId()+"");
int time = 60*60*24*7;
cookieId.setMaxAge(time);
cookieName.setMaxAge(time);
cookieId.setMaxAge(time);
response.addCookie(cookieName);
response.addCookie(cookiePwd);
response.addCookie(cookieId);
//jsp 中读取cookie
<%
Cookie cookie[] = request.getCookies();
if (cookie != null) {
for (int i = 0; i < cookie.length; i++) {
System.out.println(cookie[i].getValue());
Cookie c = cookie[i];
if (c.getValue().equals("admin")) {//查找cookie里面的是否存在cookie键位c_name的cookie
//如果存在该键 取该键对应的值==>相当于Map取值
response.sendRedirect("hello.jsp");
}
}
}
%>