You can obtain the request by asking the ActionContext or implementing ServletRequestAware. Implementing ServletRequestAwareis preferred.
Ask the ActionContext
The session attributes are available on the ActionContext instance, which is made available via ThreadLocal.
Code Block |
---|
HttpServletRequest request = ServletActionContext.getRequest();
|
Implement ServletRequestAware
Preferred
- Ensure that
servlet-config
Interceptor is included in the Action's stack.- The default stack already includes
servlet-config
.
- The default stack already includes
- Edit the Action so that it implements the ServletRequestAware interface.
- The ServletRequestAware interface expects a
setServletRequest
method. You may wish to include a companiongetServletRequest
method.
- The ServletRequestAware interface expects a
- At runtime, call
getServletRequest
to obtain a reference to the request object.
Info |
---|
When the |
Tip |
---|
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 |
Method A:
ServletActionContext.getRequest() (works internally using a ThreadLocal)
...
@see action-default.xml
@see com.opensymphony.webwork.interceptor.ServletRequestAware
@see com.opensymphony.webwork.interceptor.ServletConfigInterceptor