• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Access session object in struts1 action or formbean

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
request.getSession();

-Cameron McKenzie
 
xuebin gong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Vishal for your quick reply.
 
xuebin gong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
What's that smell? I think this tiny ad may have stepped in something.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic