Results 1 to 4 of 4

Thread: Setting a cookie via Struts

  1. #1
    Join Date
    Oct 2008
    Posts
    86

    Setting a cookie via Struts

    Hi all

    How do I go about setting a cookie via Struts? I've tried this within the perform() of an Action, and it doesn't seem to work:

    Code:
    Cookie cookie = null;
    cookie = new Cookie("u", LoginForm.getUsername());
    response.addCookie(cookie);
    cookie = new Cookie("p", LoginForm.getPassword());
    response.addCookie(cookie);
    cookie = new Cookie("g", LoginForm.getGroup());
    response.addCookie(cookie);
    
    return mapping.findForward(forward);
    Any help would be appreciated.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Setting a cookie via Struts

    The perform method? Are you using Struts 1.0? Not that it matters, cookies don't have anything to do with Struts.

    The following works for me:
    Code:
    cookie.setValue( newCookieValue );
    cookie.setMaxAge( 2592000 );///one month?
    log.debug( "adding cookie with value: " + cookie.getValue() );
    response.addCookie( cookie );
    What do you mean by it doesn't work? You don't see the value stored in your browser? IIRC you have to set an age, otherwise the cookie goes away after that browser session.

  3. #3
    Join Date
    Oct 2008
    Posts
    86

    Re: Setting a cookie via Struts

    Yes, I am using 1.0 actually.

    What I meant by it not working was that I didn't see a cookie file in my Temporary Internet Files. I'll add a max age and see if that helps.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Setting a cookie via Struts

    Okay, so I'm going out on a limb here but Struts in involved so I have to ask this: where are you looking for the cookies?

    Let me explain. If I have an Action in which I add some cookies to the HttpServletResponse and then forward to a JSP (return an ActionForward from the execute method), the JSP will not be able to see the cookies. That's because the cookies haven't yet been sent to the browser. Only on subsequent requests will the cookies be sent. A way to get around that is to specify redirect="true" in your struts-config.xml file, but then you lose your request parameters and it won't work if you "hide" your JSPs in the WEB-INF heirarchy. You have to carefully consider the various interactions in these cases. Weird things can happen if you don't.

Similar Threads

  1. cannot save Google cookie setting in Opera 10.51
    By Muthukumar in forum Technology & Internet
    Replies: 4
    Last Post: 23-02-2012, 10:04 AM
  2. Struts 1 VS Struts 2
    By PsychoVillan in forum Software Development
    Replies: 6
    Last Post: 29-03-2011, 12:23 AM
  3. Beginning with the Struts 2
    By Ronchi in forum Software Development
    Replies: 4
    Last Post: 19-03-2011, 06:32 AM
  4. JSF versus Struts
    By Daksha in forum Software Development
    Replies: 3
    Last Post: 21-07-2010, 06:20 AM
  5. Struts Forward Action with example
    By Visala28 in forum Software Development
    Replies: 2
    Last Post: 10-08-2009, 05:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,045,260.98615 seconds with 17 queries