wood burning stoves 2.0*
The moose likes Struts and the fly likes Struts2 - Getting Access to the HttpServletRequest inside the Action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 - Getting Access to the HttpServletRequest inside the Action class" Watch "Struts2 - Getting Access to the HttpServletRequest inside the Action class" New topic
Author

Struts2 - Getting Access to the HttpServletRequest inside the Action class

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,

I have seen two ways of getting access to HttpServletRequest inside the Action class

1. Using the ServletRequestAware and
2. by Using the ServletActionContext

I found the later (ServletActionContext) is very easy to use , but the author is preferring the First way .

Please let me know what is the difference and what is the preferred way .

Thanks in advance .


Save India From Corruption - Anna Hazare.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The difference is precisely what you stated: how the request is accessed.

Which is preferred, once again, is answered in the Struts 2 documentation:

http://struts.apache.org/2.1.8/docs/how-can-we-access-the-httpservletrequest.html

As to why it's preferred, consider how you'd unit test an action. If you need to inject your own request, perhaps a mock request, into the action, it's obviously going to be substantially easier if you could just set the request on the action via a setter. Using the static method means you have to create the entire Struts 2 request processing stack--not fun.

That said, tying your action to the servlet spec is almost never required, and even more rare, a good idea.

I cannot stress enough how much easier, and faster, your learning curve would be if you'd just read the documentation.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thats nice explanation .Thanks David .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2 - Getting Access to the HttpServletRequest inside the Action class