Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Member
 
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.
Reply With Quote
  #2  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 2,008
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.
Reply With Quote
  #3  
Old 25-10-2008
Member
 
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.
Reply With Quote
  #4  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 2,293
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Setting a cookie via Struts"
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


All times are GMT +5.5. The time now is 06:10 AM.