You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mike Karl <mi...@gcisoftware.com> on 2000/06/06 19:11:16 UTC
Problem Setting Cookies
I'm trying to send a cookie to the browser from a servlet. It works if
the servlets outputs something:
response.setContentType("text/html");
Cookie cookie = new Cookie("CookieTest", "CookieAREyouHERE");
cookie.setMaxAge(60);
response.addCookie(cookie);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<head>");
out.println("Cookie Set<br>");
out.println("</body>");
out.println("</html>");
But I want to send a redirect instead, and in that case the no cookie
gets sent:
response.setContentType("text/html");
Cookie cookie = new Cookie("CookieTest", "CookieAREyouHERE");
cookie.setMaxAge(60);
response.addCookie(cookie);
response.sendRedirect("/servlet/CookieTest");
The redirect works, but no cookie gets set.
thanks
mike
Re: Problem Setting Cookies
Posted by Dominik Enkelmann <en...@gmx.de>.
seems that you justcan't do this like that.
when you do the redirect, you loose everything you did to the response object
before.
to solve this problem you could pass some information with the redirect
command:
response.sendRedirect("/servlet/CookieTest?cookieinfo=CookieAREyouHERE");
and evaluate the parameter in the "CookieTest" servlet (setting a cookie in
his response object)
Dominik
Mike Karl wrote:
> I'm trying to send a cookie to the browser from a servlet. It works if
> the servlets outputs something:
>
> response.setContentType("text/html");
> Cookie cookie = new Cookie("CookieTest", "CookieAREyouHERE");
> cookie.setMaxAge(60);
> response.addCookie(cookie);
>
> PrintWriter out = response.getWriter();
> out.println("<html>");
> out.println("<body bgcolor=\"white\">");
> out.println("<head>");
> out.println("Cookie Set<br>");
> out.println("</body>");
> out.println("</html>");
>
> But I want to send a redirect instead, and in that case the no cookie
> gets sent:
>
> response.setContentType("text/html");
> Cookie cookie = new Cookie("CookieTest", "CookieAREyouHERE");
> cookie.setMaxAge(60);
> response.addCookie(cookie);
>
> response.sendRedirect("/servlet/CookieTest");
>
> The redirect works, but no cookie gets set.
> thanks
> mike
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org
Re: Problem Setting Cookies
Posted by Manea Mirko <ma...@arena.sci.univr.it>.
I think this is a bug (the same happened to me too).
See http://www.jguru.com/jguru/faq/view.jsp?EID=53251
On Tue, Jun 06, 2000 at 11:11:16AM -0600, Mike Karl wrote:
> I'm trying to send a cookie to the browser from a servlet. It works if
> the servlets outputs something:
>
> response.setContentType("text/html");
> Cookie cookie = new Cookie("CookieTest", "CookieAREyouHERE");
> cookie.setMaxAge(60);
> response.addCookie(cookie);
>
> PrintWriter out = response.getWriter();
> out.println("<html>");
> out.println("<body bgcolor=\"white\">");
> out.println("<head>");
> out.println("Cookie Set<br>");
> out.println("</body>");
> out.println("</html>");
>
> But I want to send a redirect instead, and in that case the no cookie
> gets sent:
>
> response.setContentType("text/html");
> Cookie cookie = new Cookie("CookieTest", "CookieAREyouHERE");
> cookie.setMaxAge(60);
> response.addCookie(cookie);
>
> response.sendRedirect("/servlet/CookieTest");
>
> The redirect works, but no cookie gets set.
> thanks
> mike
>
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org