6

This my code for setting new cookie

Cookie citizen = new Cookie("citizen",email);
citizen.setMaxAge(3600);
response.addCookie(citizen);

now i'm using this code for destroying the cookie

Cookie[] cookies = request.getCookies();

        for(int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("citizen")) {
             cookies[i].setMaxAge(0);
            response.addCookie(cookies[i]);
}
}

But, i'm still getting cookie value. Help will be appreciated !!

  • 1
    Do you mean that in the next request the client still sends the cookie? – Roger Lindsjö Jan 18 '12 at 09:53
  • no, i just found the above code and used it. but, it is not working. –  Jan 18 '12 at 09:55
  • How is it "not working"? Do you mean that in the next request the client still sends the cookie? If not, what happens and what do you expect to happen? – JB Nizet Jan 18 '12 at 10:15
  • It works for me. Created a page which if it gets the parameter "add" sets the cookie and if it gets the parameter "delete" it deletes the cookie (as your code above). The request after the delete contains no cookie (using Safari). Can you log what your send and gets? – Roger Lindsjö Jan 18 '12 at 10:16
  • have you checked if the String is matched correctly? Maybe your if-statement doesnt fire? – 最白目 Jan 18 '12 at 12:58

6 Answers6

4

below link might help you..

How can delete information from cookies?

Good Luck!!!

Let me know incase of any further queries...

Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
2

I was having a problem similar to this, where the cookie retained the value even after setting max age to 0 and the value to "".

I used firefox to look at the cookie attributes to help debug. When logging in, the servlet called my cookie class to set the cookie, and the cookie path was "/javawork/". When logging out, the JSP page called the same cookie class to "delete" the cookie by setting the max age to 0. But the JSP page was in a sub folder in the web app, so the when I created a cookie of the same name with max age of 0, it created a new cookie with the path "/javawork/test_login/".

So that "new" cookie immediately expired, but the original one still existed. In my delete cookie function I needed to set the path of the "new" cookie to be "/javawork/", and when I set the max age to 0 and added it, it updated the original cookie and let me properly log out.

I hope that helps.

kei23th
  • 283
  • 2
  • 6
  • 14
0

The option correct will be

Cookie cookie = new Cookie("citizen", "citizen");
cookie.setMaxAge(0);
cookie.setValue("");
response.addCookie(cookie);

if you try to get the cookie from request for next add it to response with setMaxAge(0), you could see that the cookie doesn't been removed.

markosca
  • 136
  • 6
0

This works for me -

Cookie UIDCookie = new Cookie(COOKIE_KEY, "");
UIDCookie.setMaxAge(0);
UIDCookie.setPath("/");
response.addCookie(qptUIDCookie);
advait
  • 21
  • 3
0

We can delete a cookie by setting max age as zero. For example:

Cookie[] cookies = request.getCookies();
cookies[0].setMaxAge(0);
response.addCookie(cookies[0]);

Here we delete only the first cookie

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
  • Please don't duplicate answers. https://stackoverflow.com/questions/4332258/how-can-delete-information-from-cookies/49837629#49837629 – OneCricketeer Apr 15 '18 at 06:41
0

Try to add this line:

 cookies[i].setMaxAge(0);
 //add this line
 cookies[i].setPath("/");
 response.addCookie(cookies[i]);
Anthony
  • 12,177
  • 9
  • 69
  • 105
最白目
  • 3,505
  • 6
  • 59
  • 114