Best Practise for preventing Browser Cache

Discussions

Web tier: servlets, JSP, Web frameworks: Best Practise for preventing Browser Cache

  1. Best Practise for preventing Browser Cache (1 messages)

    Hi All

    What is the best practise for preventing the browser cache without affecting any performance issue?

    My web application is using struts with ajax and using java 1.4.

    What I need to bear-in-mind?

     

    Thanks

    T.H.

    Threaded Messages (1)

  2. Browser Cache[ Go to top ]

    Hey Thomas, what i've done to prevent browser cache without having to go into each page to add the html META tags to prevent it or adding some javascript code to do that was using struts is to create a custom interceptor that automatically adds the Cache-control, Pragma and Expires attributes into the response.

    Here you have my code. Hope it helps

     

    public class CachingHeadersInterceptor implements Interceptor {

        public void destroy() {
            // TODO Auto-generated method stub
            
        }

        public void init() {
            // TODO Auto-generated method stub
            
        }

        public String intercept(ActionInvocation actionInvocation) throws Exception {
            final ActionContext context = actionInvocation.getInvocationContext();
            HttpServletResponse response = (HttpServletResponse) context.get(StrutsStatics.HTTP_RESPONSE);
            if (response != null){
                response.setHeader("Cache-control", "no-cache, no-store");
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Expires", "-1");
            }
            return actionInvocation.invoke();

        }

    }

     

    Regards!

     

    Marcos.