Cookie设置超时有关问题
Cookie设置超时问题
直接看下面的代码吧,为什么添加的cookie2设置了超时时间在超时后浏览器中的Cookie还在呢???求解???
------解决方案--------------------
不应该有问题,把浏览器重新打开再试试看
------解决方案--------------------
清除下缓存,重新打开页面。
直接看下面的代码吧,为什么添加的cookie2设置了超时时间在超时后浏览器中的Cookie还在呢???求解???
public class TestCookie extends HttpServlet {
public void service(
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
if(cookies == null) {
//添加cookie操作
Cookie cookie = new Cookie("myCookie1",
URLEncoder.encode("cookie1的内容", "utf-8"));
cookie.setPath("/testCookieAndSession/TestCookie");
//添加一个设置时间的Cookie
Cookie cookie2 = new Cookie("myCookie2",
URLEncoder.encode("cookie1的内容", "utf-8"));
cookie2.setMaxAge(30);
response.addCookie(cookie);
response.addCookie(cookie2);
out.print("Cookie已经生成");
} else {
for(Cookie c: cookies) {
String name = c.getName();
String value = URLDecoder.decode(c.getValue(), "utf-8");
out.println("name=" + name + "<br/>"
+ "value=" + value + "<br/>"
);
}
}
out.close();
}
------解决方案--------------------
不应该有问题,把浏览器重新打开再试试看
------解决方案--------------------
清除下缓存,重新打开页面。