Struts2 does'nt allow to directly access HttpServletRequest and HttpServletResponse object.
This increases the testability and test stub generating becomes easier.
However in some cases we need to directly interact with request and response.
Struts2 provides two interface (ServletRequestAware and ServletResponseAware)
which helps to get a handle of request and response objects.
This example shows accessing HttpServletRequest and HttpServletResponse in Struts2.
The necessary libraries are shown below,
The required interfaces to implement are shown below,
public abstract interface ServletRequestAware
{ public abstract void setServletRequest(HttpServletRequest paramHttpServletRequest);
}
public abstract interface ServletResponseAware
{ public abstract void setServletResponse(HttpServletResponse paramHttpServletResponse);
}
Action class implements both interfaces and keeps a reference to request and response.
Struts ServletConfigInterceptor sets both request and response objects just before invoking
action method on Action class.