我在jsp页面中使用document.cookie创建了cookie。
在我的servlet中(请记住,它不是我可以使用java脚本的jsp页面。它是一个servlet),我正在检索cookie值,在使用它之后,我想删除它们。我不想用过期时间删除曲奇。我想清除它的价值。所以,我在做cookie.setValue("");
但是,当我在浏览器中检查cookie时,它仍然保存着这个值。这不是清除。
清除之后,基本上我不希望用户看到浏览器中的cookie。
致以敬意,
发布于 2012-08-14 13:20:37
您可以通过两种方式删除cookie,要么在Servlet中(作为服务器端代码),要么如果Servlet呈现HTML文档,则可以将页面onload事件添加为javascript代码。
//服务器端代码
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
// Your task here
Cookie cookie = new Cookie("cookieName", "");
cookie.setMaxAge(0); // To delete the cookie named "cookieName", set MaxAge to 0.
response.addCookie(cookie); // You need to add this cookie to the response to tell the client (browser) that the cookie named "cookieName" must be deleted on the client (browser)
// Your task here
}对于客户端cookie删除,可以检查此http://www.webdevelopmentcentral.net/2007/12/client-side-cookie-handling.html。
https://stackoverflow.com/questions/11883995
复制相似问题