Sometimes we need to clear the browser cache or prevent the browser from caching resources so that we always get the latest output from the server.

Disabling caching of your webpages ensures your resources come from the server each and every time. In every browser, you’ll find the option to disable caching or even you can clear the cached resources but this type of caching is client side caching and we do not rely on whether users will disable cache or clear cache. Therefore we want to ensure that every time a page is requested will bring the updated content from the server.

Here is an example on how to prevent browser from caching resources.

Write Enum for declaring constants

public enum HTTPCacheHeader {

    CACHE_CONTROL("Cache-Control"),

    EXPIRES("Expires"),

    PRAGMA("Pragma"),

    private String name;

    private HTTPCacheHeader(String name) {
        this.name = name;
    }
    
    public String getName() {
        return this.name;
    }

}

The above enum constants are used for http headers. For more information on enum please read how to create enum in Java

Write the servlet filter

public class NoCacheFilter implements Filter {

    public NoCacheFilter() {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpServletResp response

        // set cache directives        httpServletResponse.setHeader(HTTPCacheHeader.CACHE_CONTROL.getName(),
                "no-cache, no-store, must-revalidate"); // HTTP 1.1
        httpServletResponse.setHeader(HTTPCacheHeader.PRAGMA.getName(),
                "no-cache"); // HTTP 1.0
        httpServletResponse
                .setDateHeader(HTTPCacheHeader.EXPIRES.getName(), 0L); // Proxies
        // pass the request along the filter chain
        chain.doFilter(request, response);
    }

    public void init(FilterConfig fConfig) throws ServletException {
    }

}

So the above filter adds http header directives for each web page and prevents caching. We are not clearing rather preventing browser from caching resources.

Make corresponding entry to the web.xml file

<?xml versi encoding "UTF-8"?>
<web-app versi
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocati>

    <filter>
        <display-name>NoCacheFilter</display-name>
        <filter-name>NoCacheFilter</filter-name>
        <filter-class>com.aas.admin.filter.NoCacheFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>NoCacheFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

We put entry of the servlet filter into web.xml file for each requested or forwarded jsp page.

That’s all. Thanks for your patience. Please leave a comment.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *