如何在Java servlet中删除cookie?
我尝试了以下方法: http://www.jguru.com/faq/view.jsp?EID=42225
编辑:下面的方法可以成功删除cookie,看起来是以下两种方法结合使用:
response.setContentType("text/html");
并且
cookie.setMaxAge(0);
之前我在做:
//remove single signon cookie if it hasn't been validated yet
response.setContentType("text/html");
Cookie cookie = new Cookie(SSORealm.SSO_COOKIE_NAME, "");
cookie.setDomain(SSORealm.SSO_DOMAIN);
cookie.setMaxAge(-1);
cookie.setPath("/");
cookie.setComment("EXPIRING COOKIE at " + System.currentTimeMillis());
response.addCookie(cookie);
根据文档,当浏览器关闭时,会使cookie过期。
负值意味着cookie不会被持久存储,并且在Web浏览器退出时将被删除。零值会导致cookie被删除。
完整的使cookie过期的代码片段如下:
//remove single signon cookie if it hasn't been validated yet
response.setContentType("text/html");
Cookie cookie = new Cookie(SSORealm.SSO_COOKIE_NAME, "");
cookie.setDomain(SSORealm.SSO_DOMAIN);
cookie.setMaxAge(0);
cookie.setPath("/");
cookie.setComment("EXPIRING COOKIE at " + System.currentTimeMillis());
response.addCookie(cookie);