File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes How to use cookies with struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to use cookies with struts" Watch "How to use cookies with struts" New topic

How to use cookies with struts

zimbu bolleddu
Ranch Hand

Joined: Jan 14, 2008
Posts: 35
Hi all,

I'm new to struts, please let me know how to use cookies with struts.

I have three struts applications, in run time I have to switch from one application to another application based on single login page. I have taught cookies are one of the solution. But while I'm googling I unable to get such a good material.

please give some example,

Thanks in advance.
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

rather then using cookies why dont you use session and then get and set function to retrieve and strore variables in the session.

Kind Regards,
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The HTTPSession won't work between different applications, since the J2EE specification mandates that different applications not share the same HTTPSession even if they're on the same server.

I'd suggest you just set a cookie with some sort of token indicating who the user is in your login action. Then have your other application check for the presence of this cookie. If it's not there, make them log in. If it is there, use the information in the cookie to get their information from a database, set it in the HTTPSession and let them continue.

Below is a link that shows you how to set and retrieve cookies. The sample code it gives is in a Servlet, but the same code could be used in a Struts Action as well.
[ July 09, 2008: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
I agree. Here's the link:
subject: How to use cookies with struts
It's not a secret anymore!