Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

RE: servlets and cookies

Steph Richardson

2003-10-14

Replies:

Randy,

Because you are doing an RequestDispatcher.include() the my.jsp page is served up in the same HTTP Response that is setting the
cookies.
my.jsp then looks in the Request for the cookies and doesn't find them there, because it's looking at the same HTTP Request that was
originally sent to your login servlet.

All the forwarding here ( or including in this case ) occurs on the server side. You need to do a round trip to the browser to set
the cookies, so that they will be included on subsequent requests. i.e. something like :
 response.sendRedirect( "/my.jsp" );

This will send a 302 response to the browser, as well as the correct response headers to set your cookie values, so that they are
included as request headers in the redirected request to my.jsp


Steph

P.S. for what it's worth I think the best explanation of all you need to know about HTTP methods, headers, cookies, etc.. comes in
the first few chapters of an O'Reilly book called "ASP in a Nutshell" - just the thing for a Tomcat users list ;)




> -----Original Message-----
> From: Randy Paries [mailto:randy.paries@(protected)]
> Sent: Tuesday, October 14, 2003 3:48 PM
> To: tomcat-user@(protected)
> Subject: servlets and cookies
>
>
> Hello
> i assume this is more of a servlet programming problem that tomcat, but
> i hope someone has some insight?
>
>
> Please tell me i can do this:
> 1)i go to a jsp page and if it does not find the exist of a cookie it
> forwards to a login screen
>
> 2) the login screen submits to a servlet, and then set some cookies and
> then forwards to the original JSP
>
> 3) the orig jsp sees those cookies and life is fine.
>
> well i can not make the servlet set the cookies so that the forwarding
> jsp sees those cookies.
>
> ====
>      Cookie cookie1 = new Cookie("USERID",_User.getUname());
>      cookie1.setMaxAge(-1);
>      cookie1.setPath("/");
>      res.addCookie(cookie1);
>
>      Cookie cookie2 = new Cookie("USERDIR",_User.getDir());
>      cookie2.setMaxAge(-1);
>      cookie2.setPath("/");
>      res.addCookie(cookie2);
>
>      RequestDispatcher disp =
> getServletConfig().getServletContext().getRequestDispatcher("/my.jsp");
>      disp.include(req,res);
>
>
> my.jsp does not see the cookies.
>
> if i just refresh the browser the second time in the cookies are there
>
> What the heck am i doing wrong
> thanks
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
> For additional commands, e-mail: tomcat-user-help@(protected)
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
For additional commands, e-mail: tomcat-user-help@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.