• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cookies

 
karim qazi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I delete a cookie in java.
I tried the following code but it does not seem to work:
public static void deleteCookie(HttpServletRequest request, String cookieName) {
Cookie currentCookie;
Cookie[] cookieList = request.getCookies(); //get cookies
if (cookieList != null) {
for (int i = 0; i < cookieList.length; i++) {
currentCookie = cookieList[i];
if (currentCookie.getName().equals(cookieName)) {
currentCookie.setMaxAge(-1);
break;
}
}
}
}
I thought setting its maxage to -1 would automatically expire it.
Any help is appreciated.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Javadocs for javax.servlet.http.Cookie, calling setMaxAge with -1 means the cookie dies when the browser is closed and 0 means to delete the cookie.
The javadocs are your friend.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!