• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

retrieving a session

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic