Issue
I trying to remove a cookie in a servlet with this code
Cookie minIdCookie = null;
for (Cookie c : req.getCookies()) {
if (c.getName().equals("iPlanetDirectoryPro")) {
minIdCookie = c;
break;
}
}
if (minIdCookie != null) {
minIdCookie.setMaxAge(0);
minIdCookie.setValue("");
minIdCookie.setPath("/");
res.addCookie(minIdCookie);
}
res.flushBuffer();
But this gives no effect and no change in the cookie properties.
I've also tried adding a cookie in this servlet and this works fine.
Why is it that I can not change the properties of an existing cookie.
Solution
The problem was that the cookie I wanted to remove had a path that was "/admin" and my logout servlet had the path "/admin/logoutServlet". When I get the cookie from the request the path is set to null. So when I add the cookie the path is set to "/admin/" as my servletIf I created a cookie with the path "/admin/" the servlet was able to remove it.
I solved the problem by explisitly setting the path of the cookie before adding it to the response.
minIdCookie.setMaxAge(0);
minIdCookie.setPath("/");
res.addCookie(minIdCookie);
But I don't understand why the path is null.
Answered By - Stefan Rasmusson
Answer Checked By - Marilyn (JavaFixing Volunteer)