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 Getting 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 "Getting "HttpSession" object" Watch "Getting "HttpSession" object" New topic

Getting "HttpSession" object

Dima Riabin

Joined: Jan 05, 2011
Posts: 16

I am porting the existing project written in JSP and cor Java (no framework used) to the use of Struts 2
The way it's written now, is that JSP use a lot of scriplets calling custom Java classes, and overal creating the mess.

I sort of successfully ported navigation to using Action classes, and now came to interesting problem, that I do not know how to resolve.

There is a mechanism written with some Helper Class, that works fine, and it's very big so I don't have to change it. I have to call the methods of this Helper class from my action classes now.
Due to poor design previously, many of the methods use "HttpSession" object in a signature. Something like

.. void load(HttpSession s, s.. ...) {

Now, from my action class, I want to do
Helper.load..(mysession, ..)...

The mysession is of type HttpSession.
I know how to use ActionAware interface, with getSession .. or ParameterAware interface with getParameter.. However,I have my session object available to me as a Map, and not as a HttpSession object. I can't figure out how to get the actual HttpSession object in Struts2 ... ActionContext.getContext() .. doesn't seem to give me any access to it

Can anyone help ?
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

I've never needed to do this but I remember when I was learning about SessionAware I think I recall you can do:


Which will get you an HttpSession
khekhone yvan
Ranch Hand

Joined: May 08, 2011
Posts: 79
Map session = ActionContext.getContext().getSession();
before that you have to store on the ssessioon like this
Map session = ActionContext.getContext().getSession();
Dima Riabin

Joined: Jan 05, 2011
Posts: 16

[quote=Jesus Mireles]ServletActionContext.getRequest().getSession()

Which will get you an HttpSession

Yes, thanks. This is what I did. I found it by browsing through Struts related class libraries for class names with the "Servlet" in them, and found this mechanism.
It is sorta covered in the JavaRanch Style Guide.
subject: Getting "HttpSession" object
Similar Threads
How to access session variable in the jsf page
Using the Strategy Pattern in Struts Actions
where to turn next? need help with model layer talking to the database
[URLyBirdHotel 1.3.3] Can I add more interfaces on
Action Class or Regular Java Class to use?