tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: cookies
Date Sun, 09 Jul 2000 19:54:10 GMT
See interspersed comment below.

Nabih Abu Abid wrote:

> Here is the source code for the servlet
> public class CookieDetector extends HttpServlet {
>
>     String probeName = "CookieDetector.probe";
>
>     public void doGet(HttpServletRequest req, HttpServletResponse res)
>         throws IOException, ServletException
>     {
>         log("doGet");
>         System.out.println("doGet");
>         Cookie cookie;
>         int port = req.getServerPort();
>         String self = req.getScheme() + "://" + req.getServerName() +
>             (port == 80 ? "" : ":"+port) + req.getServletPath();
>         log("self = " + self);
>         System.out.println("self = " + self);
>         String phase = req.getParameter("phase");
>         log("phase = " + phase);
>         System.out.println("phase = " + phase+"  probName="+probeName);
>         if (phase == null) {
>             cookie = new Cookie(probeName, req.getServletPath());
>             cookie.setComment("Will this get dropped?");
>             cookie.setMaxAge(-1);
>             cookie.setPath(req.getServletPath());
>             res.addCookie(cookie);
>             System.out.println("CAll sendRedirect");
>             res.sendRedirect( self + "?phase=check" );
>

There is a bug in Tomcat 3.1 that causes it to erase any cookies or headers you have set
when you use response.sendRedirect().  This is fixed in the current Tomcat 3.2 beta.

>         }
>         else if (phase.equals("check")) {
>             Cookie[] cookies = req.getCookies();
>             boolean hasCookies = false;
>             if (cookies != null) {
>                 for (int i = 0; i < cookies.length; i++) {
>                     System.out.println("cookie " + cookies[i].getName() + "=" +
> cookies[i].getValue());
>                     log("cookie " + cookies[i].getName() + "=" + cookies[i].getValue());
>                     if (cookies [i].getName ().equals (probeName)) {
>                         if (cookies[i].getValue().equals(req.getServletPath()))
>                         {
>                             hasCookies = true;
>                             break;
>                         }
>                     }
>                 }
>             }
>             String where;
>             String message = "";
>             if (hasCookies) {
>                 where = getInitParameter("urlCookies");
>                 if (where == null || where.equals("")) {
>                     where = null;
>                     message = "Client allows cookies";
>                 }
>             }
>             else {
>                 where = getInitParameter("urlNoCookies");
>                 if (where == null || where.equals("")) {
>                     where = null;
>                     message = "Client does not allow cookies";
>                 }
>             }
>             System.out.println("where="+where);
>             if (where == null) {
>                 res.setContentType("text/plain");
>                 res.getWriter().println(message);
>             }
>             else {
>                 res.sendRedirect(where);
>             }
>         }
>     }
> }
>
> -----Original Message-----
> From: Erik Tennant [mailto:lists@webpd.com]
> Sent: Thursday, July 06, 2000 6:16 PM
> To: tomcat-user@jakarta.apache.org
> Subject: Re: cookies
>
> Are phase 1 and phase 2 different requests?  You can't set a cookie and
> then expect to read it in the same request. Maybe you can provide a little
> more information.
>
> -Erik
>
> At 03:13 PM 7/6/00 +0200, you wrote:
> >Hi,
> >I'm  running Apache and Tomcat as a servlet runner. When i run a Cookie
> >detector servlet which builds a new cookie at phase 1 and then checks the
> >cookie at phase 2 i get null value, meaning the cookie was not build????
> >
> >If you've faced this problem before and a solution i will be very
> gratefull.
> >thanks
> >
> >

Craig McClanahan



Mime
View raw message