如何删除Java Servlet中的Cookie [英] How do you remove a Cookie in a Java Servlet
问题描述
如何删除Java servlet中的Cookie?
How do you remove a cookie in a Java servlet?
我尝试过:
http://www.jguru.com/faq/view.jsp?EID=42225
编辑:以下现在工作成功,它似乎是以下的组合:
The following now works successfully it appears to be the combination of:
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 文档。
Which expires the cookie when the browser is closed as per the documentation.
负值意味着cookie不会持久存储,并且会在Web浏览器退出时被删除。零值导致cookie被删除。
A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
到期的完整工作程式码片段是:
The full working snippet to expire a cookie is:
//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 of -1表示您想要Cookie持续会话的持续时间。您要将MaxAge设置为0。
The MaxAge of -1 signals that you want the cookie to persist for the duration of the session. You want to set MaxAge to 0 instead.
从 API文档:
负值表示Cookie不会持久存储,并且会在Web浏览器退出时被删除。零值导致cookie被删除。
A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
这篇关于如何删除Java Servlet中的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!