How to delete cookie on server using JAX-RS NewCookie?

I want to delete a cookie on the server (using a Expirespast install ). How can I do this using javax.ws.rs.core.NewCookie? I am trying to do this, but this does not work:

return Response.ok()
  .entity("hello world!")
  .cookie(
    new NewCookie(
      "foo",
      "",
      "/",
      ".example.com",
      1,
      "no comment",
      0, // maxAge
      false
    )
  )
  .build();

This snippet creates this HTTP header:

Set-Cookie:foo=;Version=1;Comment="no comment";Domain=.example.com;Path=/

This header does not remove the cookie from the server. What is the possible workaround?

+5
source share
2 answers

Here's how it works (a rather dirty approach):

return Response.ok()
  .header(
    "Set-Cookie",
    "foo=deleted;Domain=.example.com;Path=/;Expires=Thu, 01-Jan-1970 00:00:01 GMT"
  );
+8
source

I can’t try, but it should work (since for java-servlet-API to work with Java API it is enough to distribute cookies).

1. HttpServletResponse. , - :

@Context
HttpServletResponse _currentResponse;

2. cookie

Cookie userCookie = new Cookie(cookieName, "");
_currentResponse.setContentType("text/html");
userCookie.setMaxAge(0);
_currentResponse.addCookie(userCookie);
+3

All Articles