Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Redirect after addCookie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Redirect after addCookie" Watch "Redirect after addCookie" New topic
Author

Redirect after addCookie

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
I found that the response.addCookie will not work properly, if I use a redirect statement after the addCookie statement.

For an example:

response.addCookie(myCookie);
response.sendRedirect("somewhere.html");


In the above example, the cookie will never store on the browser, unless I remove the sendRedirect statement.


Any solution?
(Please don't tell me to use RequestDispatcher instead )
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Are you using Tomcat or some Tomcat based appserver? Which version? There was a bug related to this in older versions.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Originally posted by Bauke Scholtz:
Are you using Tomcat or some Tomcat based appserver? Which version? There was a bug related to this in older versions.


I'm using Tomcat 5.0
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
https://issues.apache.org/bugzilla/show_bug.cgi?id=248
https://issues.apache.org/bugzilla/show_bug.cgi?id=258

It is not clear to me when they fixed it, but the last response was dated at november 2003, shortly after 5.0 came out for first time, september 2003.

Consider upgrading to at least 5.5. I did a quick test at my Tomcat 6.0.18 and it just works fine.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
On the other hand, cookies are domain and path specific. Make sure that you don't redirect to some URL/path outside the scope of the cookie.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Bauke, Thanks for you good explanation.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Redirect after addCookie
 
Similar Threads
cookie value set problem
Why session destroyed when browser closed
How to redirect from servlet to JSP/Servlet with out executing the below code
JSP redirect
response.SendRedirect and session