File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 - Getting Access to the HttpServletRequest inside the Action class

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats nice explanation .Thanks David .
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic