如何在Java Servlet中删除cookie?
我尝试了这个:http : //www.jguru.com/faq/view.jsp?EID=42225
编辑:以下内容现在可以成功运行,它似乎是以下各项的组合:
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);
MaxAge -1表示你希望cookie在会话期间持续存在。你想将MaxAge设置为0。
从API文档中: