This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Access session object in struts1 action or formbean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Access session object in struts1 action or formbean" Watch "Access session object in struts1 action or formbean" New topic
Author

Access session object in struts1 action or formbean

xuebin gong
Greenhorn

Joined: May 02, 2008
Posts: 4
I am working on a web project by using struts 1, I do not know how to access session object in actionform or action, excluding in execute() mathod in action class.

Thanks
Xuebin
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
In order to access session, you need access to object HttpServletRequest.

ActionForm does not have access to HttpServletRequest hence you cant access , session over here.
In Action class, only execute method is passed object HttpServletRequest, hence apart from execute, its not possible to access session.

However following places you can access session variable using request object.
see Action class documentation to find what method have access to request object.
There are more than 10 methods like that.

HTH
V
[ May 02, 2008: Message edited by: Vishal Matere ]

SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

request.getSession();

-Cameron McKenzie
xuebin gong
Greenhorn

Joined: May 02, 2008
Posts: 4
Thank you Vishal for your quick reply.
xuebin gong
Greenhorn

Joined: May 02, 2008
Posts: 4
Thanks Cameron,

But only in execute() we can get request. I am integrating Ajax DWR1.0 into struts. When JSP is triggered by drop down list, a xmlHTTPrequest is sent back to server. I mapped a method in action class to process data. I need the information in session object to access data base and then return the data back to next drop down list in same JSP. But How do I access session object from this method (not execute() in action).
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
Originally posted by xuebin gong:
I mapped a method in action class to process data


Can you tell me, method signature for this method?
I would say pass HttpServletRequest object to this method , thus you can access session object from there.

You can use request processor to pass Request object to your method

HTH
V
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you look closely at the DWR documentation, you will see that if you declare a parameter of type HTTPServletRequest or HTTPSession on your method and declare the method in the DWR config file, the DWR servlet will pass these objects as parameters when it calls your method.


Merrill
Consultant, Sima Solutions
xuebin gong
Greenhorn

Joined: May 02, 2008
Posts: 4
Thanks Vishal, Merill.

The problem is solved by retriving request object from Ajax xmlHttpServletRequest.

There are two ways to get request object.

(1)Put request parameter in the Ajax DWR server method

getComByDiv(HTTPServletRequest request, HttpServletResponse response, String div)

{
......
HttpSession session = request.getSession();
......
}

(2)In the Ajax DWR server method, get session from WebContextFactory

HTTPSession session = WebContextFactory.get().getSession();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access session object in struts1 action or formbean
 
Similar Threads
html:link tag ------- urgent
session listener
storing an object into session variable
Threads & Helper Class
Accessing the ActionForm inside a Servlet Filter