This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieving a session

 
D. Clarke
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic