Easy one!

Reason:

It's because when you use a forward, you are not actually making a trip back to the 
client, so the new cookie has not made it to the browser yet and the JSP that you have 
forwarded to get's the last known state sent by the browser, which was before you 
generated the cookie.

Code Fragment:

        if (databaseTransactionCompleted) {
            //
            // Put the username into a cookie.
            //
            Cookie c = new Cookie(SessionData.COOKIE_USERNAME, 
logonData.getUsername());
            c.setMaxAge(-1); // Session only cookie
            response.addCookie(c);

            forward = mapping.findForward(SUCCESS);
            forward.setRedirect(true);
        } else {
            forward = mapping.findForward(FAILURE);
        }
        return (forward);
    }

The relevent line here from this fragment out of a logon action that I wrote is the 
"forward.setRedirect(true)" line. Now, I'm sure that in version 1.1 there is another 
way to do that, I seem to recall that there is an extra attribute that you can set 
somewhere in the struts-config.xml, but this is how I did it under version 1.0.

However you do it, make certain that when you set/change/remove a cookie, that you go 
back to the browser before you go anywhere else.

Simon

-----------------------------------------------------------------
Simon P. Chappell                     [EMAIL PROTECTED]
Java Programming Specialist                      www.landsend.com
Lands' End, Inc.                                   (608) 935-4526


>-----Original Message-----
>From: Brian Holzer [mailto:[EMAIL PROTECTED]]
>Sent: Thursday, June 06, 2002 5:50 PM
>To: <
>Subject: Struts and cookies
>
>
>Hi all,
>   I am hoping that this is an easy question.  I have ActionA, 
>jspA and ActionB.  In ActionA I add a cookie to the response 
>passed into the perform() method.
>
>     Cookie cookie1 = new Cookie( "SGI", "autodev01" );
>     response.addCookie( cookie1 );
>
>  Then I exit ActionA with the following command
>
>     return( mapping.findForward( fwd ) ); 
>
>I am then forwarded to jspA where I enter some search criteria 
>and press the submit button which forwards to ActionB.  In 
>ActionB I use some displays to show the contents of the 
>request passed into the perform() method 
>
>      System.out.println( "OUTPUTTING HEADERS: " );
>       Enumeration enum = request.getHeaderNames();
>       while ( enum.hasMoreElements() ) {
>          String k = (String) enum.nextElement();
>          Object v = request.getHeader( k );
>          System.out.println( "Key: " + k + "  Value: " + 
>v.toString() );
>       } // end of while
>
>     Cookie sgiCookie[] = request.getCookies();
>     System.out.println( "OUTPUTTING COOKIES: " );
>     int w = 0;
>     while( w < sgiCookie.length ){
>        System.out.println( "Cookie number " + w );
>        System.out.println( "Cookie name: " + sgiCookie[w].getName() );
>        System.out.println( "Cookie value: " + 
>sgiCookie[w].getValue() );
>        System.out.println( "---------------------------" );
>        w++;
>     }
>
>but the cookie I added is not there.  The only cookie still 
>there is the jsessionid cookie.  I am using Silverstream 3.7.3 
>app server.
>
>Does anyone know why my cookie is dissappearing?  or am I 
>somehow not adding it in the right place? 
>
>Thanks
>Brian
>
>
>--
>To unsubscribe, e-mail:   
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to