Java Servlet: las cookies no se eliminan

I'm having an issue with deleting cookies from my servlet code. Given bellow is my code.

private void clearCookies(HttpServletRequest req, HttpServletResponse resp) {
    Cookie[] cookies = req.getCookies();

    for (Cookie curCookie : cookies) {          
        curCookie.setValue(null);
        curCookie.setMaxAge(0);
        curCookie.setPath("/");
        resp.addCookie(curCookie);          
    }
}

I do a resp.sendRedirect(url) after this method call. However, not all cookies get deleted, for example this cookie never get deleted.

Name:   reqURI
Content:    ../../webapp/index.jsp
Domain: mgt.appserver.com
Path:   /
Send for:   Any kind of connection
Accessible to script:   Yes
Created:    Tuesday, November 26, 2013 4:35:19 PM
Expires:    When the browsing session ends

Does anyone knows what I'm missing here? I read the Java Cookie object documentation and according to that value 0 should make the cookie to be removed. But it's not. And I tried many more suggestions and none of it worked. I tried this with Google Chrome and Firefox, so can't believe it's an issue with the browsers. I have no idea why such a generic thing is not properly documented and complected in a language like Java.

preguntado el 27 de noviembre de 13 a las 01:11

2 Respuestas

Actualizar

Según Problema al eliminar la cookie en el servlet

La ruta y el dominio siempre serán nulos cuando recupere cookies en Java porque solo son necesarias en la respuesta del navegador del cliente. Sin embargo, si está en el mismo dominio de seguridad (independientemente de la ruta), aún tiene los derechos para eliminarlos. Desafortunadamente, debido a que la ruta no está incluida, no puede eliminar la cookie ahora sin conocer explícitamente esa ruta. Simplemente usando el mismo nombre de cookie, pero una ruta diferente no funcionará. Esas se consideran dos cookies diferentes, y encontrará que en lugar de eliminar la cookie, acaba de crear otra en una ruta diferente.

So you should not change value or path as this will create a new cookie

contestado el 23 de mayo de 17 a las 13:05

This code fixed the issue, I think creating a brand new cookie and adding it to the response fixed the issue. The path should be exactly the same as original value. Setting maxAge to 0 is important as well. Cookie cookie = new Cookie("reqURI", null); cookie.setPath("/"); cookie.setMaxAge(0); resp.addCookie(cookie); - seguro

Where are you redirecting? The response has to be committed back to the host that created the cookie in the first place in order for it to be removed. Also, you don't need to set the value to null.

respondido 27 nov., 13:02

Same webapp set the cookie and removing from a different servlet in the same webapp. - seguro

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.