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 );
精彩评论