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

Access session object in struts1 action or formbean

 
xuebin gong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getSession();

-Cameron McKenzie
 
xuebin gong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Vishal for your quick reply.
 
xuebin gong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
xuebin gong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic