wood burning stoves 2.0
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
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: 18

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: 18

[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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Getting "HttpSession" object
It's not a secret anymore!