Several questions have been asked on how to delete a cookie from the request that a servlet send to a client web page.
Below is my code snipet which doesn't work as in all the other email's on this site concerning this subject (taken from Hans Bergen blog page):
Funny thing was that the cookie I wanted to delete it didn't original exist until I tried to delete it. Below is the output my code snippet:
if you set cookie expire time properly they will automatically disappears once particular time is elapsed. For what purpose you want delete the cookies is there any genuine reason or just for exploring
Cookies are made to hold the session id, It is the containers which uses the cookies to bind between session object and particular client. that means when the container receives first request from the client then in the response, session id will be added and this session id will be saved in the cookies in the client machine. then in the next request from the client. the browser adds the cookies in the request before submitting the request to the server. Again the cycle repeats container finds the session id in the cookie and binding happens between session object and the client.
I hope that helps and java experts correct me if I am wrong.
To delete a cookie you need to set the same 'namespace' as the existing cookie. The namespace is composed of domain, path and protocol (secure/non-secure). These values are only stored on the client side. The browser requires these information to select all the cookies which suits the HTTP request. Domain, path and protocol must match or it will be treat as 2 different cookies even if the name is equal.
The 2 cookies have the same name, but use a different domain.
Cookie1 will be sent only to server myserver.company.com
Cookie2 will be sent to all server within the domain *.company.com
If you want to set/delete Cookie2 you need to set domain (by default it is set to the hostname).
The above solution works fine if you work with the default values (path=/ domain=hostname) or leave it blank. If not you need to set the correcsponding values manually.
The problem is you can't find out the 'namespace' of a cookie on the server side. The browser uses this information only to select the matching cookies, but doesn't send the information about the namespace.
myCookie.getDomain() and myCookie.getPath() will always return 'blank'.
To delete a cookie you need to know this information or you will just try to delete a cookie, which doesn't exist.
Hope this helps
Joined: Oct 03, 2008
thanks for the reply by Lokesh and Malatsh:
First, why do I want to delete cookies. I'm allowing the user to set the timezones they want to view in my time zone conversion tool. I will attach screen shoot. I want to say the user's preferences. Because
Some interesting comments by both. I modified my code to (1) try and set path, domain, etc and (2) view these settings. But as mentioned in the reply I don't can print out what the client is saving for the domain and path.
Ok, code below shows what I'm trying to do. Set name, value, path, domain and secure attributes of cookie then delete it. Still can't delete. I also included a printout of my results below:
The output of this code is:
So even if I set path and domain, I can't confirm what my "domain name space" is for a cookie.
Let me ask, can somebody send me some code snippet where they actually deleted a cookied when view with Internet Explorer?
subject: How to delete a cookie from Request (age old question never answered)