Now coming back to our clear cache implementation, we need to execute the setHeader() method on response object, but 

we need to have the response object. BUT HOW ?
The answer is in the method signature :

public String intercept(ActionInvocation invocation)

To get response object we will make use of invocation object reference. Here is how.

ActionContext ctx=invocation.getInvocationContext();

ActionContext is the context in which the actions execute. So it has encapsulated many behavior and properties that represents the action execution context.

ActionContext context=(ActionContext)invocation.getInvocationContext();
HttpServletResponse response=(HttpServletResponse)context.get(StrutsStatics.HTTP_RESPONSE);


And we are done, finally my class looks like this:

package com.bullraider.apps.interceptors;

import javax.Servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ClearCacheInterceptor  extends AbstractInterceptor{
    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext context=(ActionContext)invocation.getInvocationContext();
        HttpServletResponse response=(HttpServletResponse)context.get(StrutsStatics.HTTP_RESPONSE);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        String result=invocation.invoke();
        return result;


One more important thing we skipped here is that 1st property of interceptor. When the interceptor is in pre processing state(before executing the next interceptor or action), it executes the lines of code that comes before the call to invocation.invoke() in the intercept method. And in post processing state it execute the lines comes after the invoke method.

In our code it seems meaningful only when we should have the code after the invocation.invoke() because the response is given back only in the post processing time. But If you try recollecting the Servlets request and response model, then it becomes clear that no matter where you keep the our code (before or after the call to invoke()).It does the same thing. So the 1st property of interceptor doesn’t apply to us in this situation.

Lets now stitch our interceptor in struts.xml.

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    <package name="test" extends="struts-default" >
            <interceptor name="clear-cache"
                class="com.bullraider.apps.interceptors.ClearCacheInterceptor" />
        <action name="Test" class="com.bullraider.apps.actions.TestAction" >
            <interceptor-ref name="clear-cache" />
            <result name="success">/success.jsp</result>


Lets have a look at the action element, there is an element <interceptor-ref> and name with value clear-cache. What it means is ; apply this interceptor clear-cache in this action named Test.

The "clear-cache" is defined inside the <interceptor> element with value "clear-cache" and class as "com.bullraider.apps.interceptors.ClearCacheInterceptor". And this declaration has to be kept inside the <interceptors> element. We will have more discussion in the 3rd tutorial later.

And for obvious reason I am not going to discuss about the action class TestAction as follows.

package com.bullraider.apps.actions;

public class TestAction {
    public String execute(){
        System.out.println("Inside Action");
        return "success";

Download  cacheinterceptorexample.war  and run in your container to test it. Well if you are wondering how will you know if the headers applied in the response i.e success page. I use firebug extension for Firefox.

Don't Forget to take the quick quiz on this tutorial on th top of this page