We help IT Professionals succeed at work.

Deleting Cookies in JSP

DoughBoy
DoughBoy asked
on
Hi All,

 Could you please give me some sample code of how to delete cookies in Java Server Pages?

TIA,
DoughBoy

Comment
Watch Question

Commented:
Hi,

You should use your "request" object to get the Cookie object array:

<%
  Cookie[] myCookies = request.getCookies();
%>

Then you can loop through the array and manipulate your "javax.servlet.http.Cookie" objects.

Regards,

    Yagiz Erkan
    http://www.erkans.com
    http://groups.yahoo.com/group/ServerSideJ (ServerSideJ-subscribe@yahoogroups.com)
Commented:
To delete cookies, just sets the maximum age of the cookies in 0 second.

<%
Cookie[] myCookies = request.getCookies();

for (int i = 0; i < myCookies.length; i++) {
  if (myCookies[i].getName().equals("deletedCookieName")) {
    myCookies[i].setMaxAge(0);
  }
}
%>

Author

Commented:
thanks very much

Explore More ContentExplore courses, solutions, and other research materials related to this topic.