|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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); |
#2
| |||
| |||
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 ); |
#3
| |||
| |||
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
| |||
| |||
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. |
![]() |
|
Tags: cookie, struts |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
cannot save Google cookie setting in Opera 10.51 | Muthukumar | Technology & Internet | 4 | 23-02-2012 10:04 AM |
Struts 1 VS Struts 2 | PsychoVillan | Software Development | 6 | 29-03-2011 12:23 AM |
Beginning with the Struts 2 | Ronchi | Software Development | 4 | 19-03-2011 06:32 AM |
JSF versus Struts | Daksha | Software Development | 3 | 21-07-2010 06:20 AM |
Struts Forward Action with example | Visala28 | Software Development | 2 | 10-08-2009 05:14 PM |