Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

You can obtain the request by asking the ActionContext or implementing ServletResponseAware. Implementing ServletResponseAware is preferred.

Ask the ActionContext

The response is available on the ActionContext instance, which is made available via ThreadLocal.

Code Block
HttpServletResponse response = ServletActionContext.getResponse();

Implement ServletResponseAware

(star) Preferred

  • Ensure that servlet-config Interceptor is included in the Action's stack.
    • (info) The default stack already includes servlet-config.
  • Edit the Action so that it implements the ServletResponseAware interface.
    • The ServletResponseAware interface expects a setServletResponse method. You may wish to include a companion getServletResponse method.
  • At runtime, call getServletResponse to obtain a reference to the response object.
    Info

    When the servlet-config Interceptor sees that an Action implements ServletResponseAware, it passes a reference to the request the Action's setServletResponse method.

    Tip

    It is more difficult to test Actions with runtime dependencies on HttpServletReponse. Only implement ServletResponseAware as a last resort. A better approach to solving a use case involving the response may be with a custom Result Type.

    @see struts-default.xml
    @see org.apache.struts.action2.interceptor.ServletResponseAware
    @see org.apache.struts.action2.interceptor.ServletConfigInterceptor