0

I have read this below para from a website :

When a Action class requires objects such as the HttpServletRequest ,that can be obtained by asking the ActionContext or implementing ServletRequestAware. 

Could anybody please tell me any scenario where a Action class requires HttpServletRequest object ??

Thank you very much .

Roman C
  • 49,761
  • 33
  • 66
  • 176

2 Answers2

0

I myself see very little or no use of binding your Action with HttpServletRequest object.I believe idea is to provide a way to access these objects in some specific use-case (which i myself have not seen so far)

It is more difficult to test Actions with runtime dependencies on HttpServletRequest.I myself looking forward if some one can come up with any such use-case where its like must to get a reference of HTTP object in Action Class as using such reference are against Struts2 philosophy of POJO action classes.

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
  • Thank you very much , if using HttpServletrequest is the rare case , then how can we get access to HttpSession , (Is it not the way through HttpServletRequest ) –  Sep 13 '11 at 10:52
  • Implementing SessionAware is preferred although you can access the session in other ways.By implementing SessionAware you give struts2 a chance to inject the session attributes (Which is the map) into your action. This keeps your code clean from coupling to underlying servlet context and also makes testing easier since you can pass any map object to your action with desired attributes. Also any change made to this session map will be reflected on the real session object. Map attibutes = ActionContext.getContext().getSession(); this is other way to get access to session – Umesh Awasthi Sep 13 '11 at 10:56
0

Any time you need access to anything in the request other than the parameters or attributes. For example, you might want the method, the remote user, whatever.

As Umesh said, these are edge-cases. More often that kind of information is needed only in an interceptor, which can then set it on an action in a less-dependent way.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302