0

I have created cookie using document.cookie in a jsp page.

In my servlet (remember its not a jsp page where I can use java script. Its a servlet), I am retrieving cookie value and after its use, I want to delete them. I dont want to delete the cookies by using expiry time. I want to clear off its values. So, I am doing cookie.setValue("");

But, when I check the cookie in my Browser, its still holding the value. Its not clearing off.

  1. How to clear off its value?
  2. Also, is there a way to clear off the cookie name? I know that there is no cookie.setName() function. So, any other means?

After clearing, basically I dont want a user to see the cookies in the browser.

Regards,

4

1 に答える 1

0

2 つの方法で Cookie を削除できます。サーブレットで (サーバー側のコードとして)、またはサーブレットが HTML ドキュメントをレンダリングする場合は、ページのオンロード イベントを 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 を確認してください。

于 2012-08-14T13:20:37.793 に答える