将cookie值设置为null由document.cookie创建

时间:2012-08-09 12:58:24

标签: cookies

我在jsp页面中使用document.cookie创建了cookie。

在我的servlet中(记住它不是我可以使用java脚本的jsp页面。它是一个servlet),我正在检索cookie值,在使用之后,我想删除它们。我不想使用到期时间删除cookie。我想清除它的价值观。所以,我正在做cookie.setValue("");

但是,当我在浏览器中检查cookie时,它仍然保持着值。它没有清理。

  1. 如何清除其价值?
  2. 另外,有没有办法清除cookie名称?我知道没有cookie.setName()函数。那么,还有其他任何手段吗?
  3. 清除后,基本上我不希望用户在浏览器中看到cookie。

    此致

1 个答案:

答案 0 :(得分:0)

您可以通过Servlet(作为服务器端代码)以2种方式删除cookie,或者如果您的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