This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes retrieving a session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "retrieving a session" Watch "retrieving a session" New topic
Author

retrieving a session

D. Clarke
Greenhorn

Joined: May 09, 2006
Posts: 25
Good day -

Using Struts 1.2.8 I have some data that I need persisted (such as user and site profile) that I need access to throughout the application. I can easily stored this in the Session, and that works fine in the action classes where I have a hook to the session.

My question is how is the best way to retrieve the session from plain java classes without passing around the session or the session stored class through out the application?

Thanks,
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Your only access to the HttpSession object from your Struts application is through the HttpServletRequest object that is passed to the execute() method of your Action class. From there you get the HttpSession object using the request's getSession() method. Once you have the session, you can extract whatever objects you need from it, or pass it as a parameter to other classes.

There is no "plain java" way to get the HttpSession object.

The J2EE web container does a lot of work behind the scenes to determine if the user already has a session cookie or a session ID passed through URL rewriting and to then find the HttpSessoin object if it exists. Once the container has found the session, it passes the information through to the service() method of the ActionServlet, and the ActionServlet then passes it on to your Action class. You can't just find the session in a Java class without going through the web container. The HttpServletRequest object passed to your Action class is your window to the container.


Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieving a session
 
Similar Threads
How to use an Admin and User class in a Web Application
HttpSession = total confusion
Getting Date & Time from the SessoinID
accessing session variable values in java class
JSP Queries