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?
Joined: Feb 15, 2005
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.