JSP/servlet: remove cookies?

K

Kai Grossjohann

The HttpServletResponse class allows me to call addCookie to add a
cookie to the response. But how do I tell the browser that it should
forget a cookie that it already has?

Currently I'm using a special value to mean "forget this cookie" which
is checked for when the cookie is used. But I'm thinking that can't
be the right approach. (My cookie values are positive integers, so
I'm using -1 as the "forget it" value.)

Kai
 
S

Sudsy

Kai said:
The HttpServletResponse class allows me to call addCookie to add a
cookie to the response. But how do I tell the browser that it should
forget a cookie that it already has?

Currently I'm using a special value to mean "forget this cookie" which
is checked for when the cookie is used. But I'm thinking that can't
be the right approach. (My cookie values are positive integers, so
I'm using -1 as the "forget it" value.)

Kai

You should really learn to search before posting.
Suppose I want to delete a cookie with a name of "FRED".
Here's the code:

Cookie cookie = new Cookie( "FRED", "" );
cookie.setMaxAge( 0 );

Now just invoke HttpServletResponse#addCookie.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top