To use the second option, the HelloWorld action needs to implement ServletResponseAware interface. This interface defines a single method,void setServletResponse(HttpServletResponse response), which the framework, ServletConfigInterceptor to be precise, uses to carry out interface injection into the action at runtime. Here is how the code looks like -

So far, so good. These options will allow us to do the needful. Still, I'm a little uncomfortable using any of these. Reason? Well, both options will tie up the actions to servlet environment, which , amongst others, will make unit testing difficult. Do we have a better option? Yes, in my opinion, we can use the interceptors feature of the Struts 2 framework to our advantage, to abstract this functionality into a separate interceptor and decouple the actions from Servlet API. More on it in the next post. Thanks for reading and stay tuned.