开发者

Adding cookie in java and then HTTP redirect doesnt show cookie in client side

开发者 https://www.devze.com 2023-01-30 15:39 出处:网络
I have requirement where in i need to add cookie in java and then redirect it to different URL. Now this url process should persist the cookie which i set and after its processing send it back to clie

I have requirement where in i need to add cookie in java and then redirect it to different URL. Now this url process should persist the cookie which i set and after its processing send it back to client. The code goes 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);
respon开发者_JS百科se.sendRedirect("someNewUrl");

Please help me regarding how can i persist the cookie throughout the redirect lifecycle and to the client. Thanks in advance.


Try to actually add the cookie to the response:

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

See also:

  • javax.servlet.http.HttpServletResponse.addCookie(javax.servlet.http.Cookie)


Did you add the cookie to the response? I'm seeing the code that just creates the cookie.

Try this :

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

精彩评论

暂无评论...
验证码 换一张
取 消