Adding cookie in java and then HTTP redirect does not display cookie on client side

I have a requirement when I need to add a cookie in java and then redirect it to another URL. Now this url process should be saved in the cookie that I set, and after processing it, send it to the client. The code is as follows

Cookie cookie = new Cookie("name", "value")
// To make sure cookie is established for all the url paths
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
response.sendRedirect("someNewUrl");

Please help me on how I can save cookies throughout the redirect life cycle and for the client. Thanks in advance.

+3
source share
2 answers

Try adding a cookie to the answer:

Cookie cookie = new Cookie("user", "anonymous");
response.addCookie(cookie);

See also:

+2
source

cookie ? , cookie.

:

 Cookie c = new Cookie(name,value);
    c.setMaxAge( 3 * 30 * 24 * 60 * 60 );
    c.setPath( "/" );
    response.addCookie( c );
+1

Source: https://habr.com/ru/post/1780567/


All Articles