Fabio Olivetto
Fabio Olivetto

Reputation: 616

Java Servlet not deleting cookies

In a servlet i have the following code that deletes cookies.

Cookie[] arr = request.getCookies();
    for(Cookie y:arr){
        y.setMaxAge(0);
        y.setPath("/");
        response.addCookie(y);
}

In a jsp file i have this jstl code that it's supposed to show me the present cookies in the server

<c:forEach var="c" items="${cookie }">
<table border=1>
<tr>
    <td>${c.value.name }</td>
    <td>${c.value.value }</td>
</tr>
</table>
</c:forEach>

When i add cookies they are properly displayed in the jsp, but when i delete them with the first code in the servlet and i reload the page they are still there, is it something wrong with the java code or is the jsp jstl not reloading properly

Upvotes: 0

Views: 88

Answers (1)

Sumanth Jois
Sumanth Jois

Reputation: 3234

I had encountered the same problem, for me this code worked

Cookie[] cookies = req.getCookies();
if (cookies != null)
    for (int i = 0; i < cookies.length; i++) {
        cookies[i].setValue("");
        cookies[i].setPath("/");
        cookies[i].setMaxAge(0);
        resp.addCookie(cookies[i]);
    }

Thankyou, I hope this works for you too.

Upvotes: 1

Related Questions