It's not a secret anymore!
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
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "retrieving a session" Watch "retrieving a session" New topic

retrieving a session

D. Clarke

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?

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.

Consultant, Sima Solutions
I agree. Here's the link:
subject: retrieving a session
It's not a secret anymore!