我自己写的登出代码:
public void logout(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
// 从session中将user删除
session.removeAttribute("user");
// 将存储在客户端的cookie删除掉
Cookie cookie_username = new Cookie("cookie_username", null);
cookie_username.setMaxAge(0);
cookie_username.setPath(request.getContextPath());
// 创建存储密码的cookie
Cookie cookie_password = new Cookie("cookie_password", null);
cookie_password.setMaxAge(0);
cookie_password.setPath(request.getContextPath());
response.addCookie(cookie_username);
response.addCookie(cookie_password);
try {
response.sendRedirect(request.getContextPath() + "/index.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
当我把 cookie_username.setPath设置为"/"的时候,登出就没有效果了,有没有大神能告诉我是为什么吗