The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


cookie lost while sendRedirect

Posted by Divya on January 30, 2002 at 5:38 PM

> Hi all, this is the case, hope to get help from you:

> 1. add a cookie to response
> HttpServletResponse.addCookie(Cookie)

> 2. then, redirect it to a new URL
> HttpServletResponse.sendRedirect("the URL")
> I have made sure the new URL is in the same domain.
> This is tested on Tomcat, SSL is not set up, not a public URL.

> Results:
> The cookie is not add to client(my local machine).


The cookies stored in the response object would be lost once you use sendRedirect. You'll have to use RequestDispatcher through which you can forward your Request and Response objects.

String path = "/servlet/yoururl?parameters"
ServletContext ctx = getServletContext();
RequestDispatcher rd = ctx.getRequestDispatcher(path);
rd.forward(req, res); //where res is the Response object in which you have set the cookie



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us