java 如何从cookies中删除信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4332258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can delete information from cookies?
提问by Nikki
i have used webservice into my application and want to delete information from cookies that is saved on one state and must be deleted on another state on particular condition given. How can i do so? Thanks
我已经在我的应用程序中使用了 web 服务,并希望从保存在一个状态的 cookie 中删除信息,并且必须在给定的特定条件下在另一个状态下删除信息。我该怎么做?谢谢
回答by Singleton
check http://www.ehow.com/how_5169279_remove-cookies-java.html
检查 http://www.ehow.com/how_5169279_remove-cookies-java.html
How can I delete a cookie from within a JSP page?
如何从 JSP 页面中删除 cookie?
A cookie, mycookie, can be deleted using the following scriptlet:
可以使用以下脚本删除 cookie mycookie:
<%
Cookie killMyCookie = new Cookie("mycookie", null);
killMyCookie.setMaxAge(0);
killMyCookie.setPath("/");
response.addCookie(killMyCookie);
%>
How do I delete a cookie set by a servlet?
如何删除由 servlet 设置的 cookie?
Get the cookie from the request object and use setMaxAge(0)
and then add the cookie to the response object.
从请求对象中获取 cookie 并使用setMaxAge(0)
,然后将 cookie 添加到响应对象中。
回答by Codemaker
You can delete or unset cookies in JSP by setting setMaxAge()
for the cookie to zero.
For example:
您可以通过将setMaxAge()
cookie 设置为零来删除或取消设置 JSP中的 cookie。例如:
Cookie[] cookies = request.getCookies();
cookies[0].setMaxAge(0);
response.addCookie(cookies[0]);
Here we collect all cookies and delete the first cookie by setting its age to zero.
在这里,我们收集所有 cookie 并通过将其年龄设置为零来删除第一个 cookie。