I’m doing something very simple that’s just not working.
I have an html page with a button that’s supposed to logoff and delete the login cookie, when pressed, it /runs this code in a servlet:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
deleteCookie(req, resp);
resp.sendRedirect("login");
}
private void deleteCookie(HttpServletRequest req, HttpServletResponse resp) {
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (CID_KEY_NAME.equals(cookie.getName())) {
cookie.setMaxAge(0); // Should make the browser delete the cookie
cookie.setValue(null);
resp.addCookie(cookie);
}
}
}
}
the sendRedirect("login") sends me to a new servlet that’s responsible for the log-in but when I arrive at the new servlet and check for the existence of the cookie above, I can see it exists with the maxage value of -1
Am I doing something wrong?
The solution for IE was that I should have set the Path for both setting the cookie and removing it.
Chrome is ok with having no path.