This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization 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
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats nice explanation .Thanks David .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic