DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
You can obtain the request by asking the ActionContext or implementing ServletRequestAware. Implementing ServletRequestAware is preferred.
Ask the ActionContext
The request is available on the ActionContext instance, which is made available via ThreadLocal.
| Code Block |
|---|
HttpServletRequest request = ServletActionContext.getRequest(); |
Implement ServletRequestAware
Preferred
- Ensure that
servlet-configInterceptor is included in the Action's stack.The default stack already includes
servlet-config.
- Edit the Action so that it implements the ServletRequestAware interface.
- The ServletRequestAware interface expects a
setServletRequestmethod. You may wish to include a companiongetServletRequestmethod.
- The ServletRequestAware interface expects a
- At runtime, call
getServletRequestto obtain a reference to the request object.Info When the
servlet-configInterceptor sees that an Action implements ServletRequestAware, it passes a reference to the request the Action'ssetServletRequestmethod.
@see struts-default.xmlTip It is more difficult to test Actions with runtime dependencies on HttpServletRequest. Only implement ServletRequestAware as a last resort. If the use case cannot be solved by one of the other
servet-configinterfaces (ApplicationAware, SessionAware, ParameterAware), consider whether an custom Interceptor could be used instead of Action code. (Review howservlet-configworks for examples of what can be done.)
@see org.apache.struts.action2.interceptor.ServletRequestAware
@see org.apache.struts.action2.interceptor.ServletConfigInterceptor