This blog and domain is up for sale, contact us at street_demons@rocketmail.com

Monday, 21 July 2014

Enable caching in tomcat 6

In previous tutorial we learnt how to enable caching in tomcat 7. In this tutorial we will learn how to enable caching in tomcat 6. Tomcat 6 doesn't comes with inbuilt ExpiresFilters like in tomcat 7, but we can write our own filter to achieve caching. Two headers "Cache-control" and "Expires" are needed to be set so that browser can determine when to load resource from server or cache.

Configuring web.xml

We will first modify our web.xml deployment descriptor so that we can invoke our filter class for respective resources. Add the following code and modify the <param-value>7200</param-value>  to any value according to your need. The value is specified in seconds. Here it is 7200 means cache resources for 2 hrs.

<filter>
 <filter-name>CacheFilter</filter-name>
 <filter-class>utility.CacheFilter</filter-class>
 <init-param>
  <param-name>Cache-Control</param-name>
  <param-value>7200</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>CacheFilter</filter-name>
 <url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
 <filter-name>CacheFilter</filter-name>
 <url-pattern>*.css</url-pattern>
</filter-mapping>
<filter-mapping>
 <filter-name>CacheFilter</filter-name>
 <url-pattern>*.gif</url-pattern>
</filter-mapping>
<filter-mapping>
 <filter-name>CacheFilter</filter-name>
 <url-pattern>*.png</url-pattern>
</filter-mapping>
<filter-mapping>
 <filter-name>CacheFilter</filter-name>
 <url-pattern>*.ico</url-pattern>
</filter-mapping>
<filter-mapping>
 <filter-name>CacheFilter</filter-name>
 <url-pattern>*.jpg</url-pattern>
</filter-mapping>
<filter-mapping>
 <filter-name>CacheFilter</filter-name>
 <url-pattern>*.jpeg</url-pattern>
</filter-mapping>

CacheFilter class

This class will intercept every request specified by url pattern for this filter in web.xml, We will see it later. It sets the "Expires" and "Cache-control" headers to time specified in seconds in Filter param value.

package utility;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * HTTP Cache fileter for tomcat 6
 * @author Saket kumar
 *
 */
public class CacheFilter implements Filter {

     private final static String    KEY = "Cache-Control";
     private final static String    PRAGMA = "Pragma";
     private final static String    EXPIRES = "Expires";
     
     private String Lifetime = null;
     
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
 
         if (Lifetime != null)
         {
          long value = Long.parseLong(Lifetime);
          
             ((HttpServletResponse) res).setHeader(KEY, "max-age="+value+", public");
             ((HttpServletResponse) res).setHeader(PRAGMA, null);
             
             
             final long DURATION_IN_MS = value* 1000;
             
             long now = System.currentTimeMillis();
             ((HttpServletResponse) res).setDateHeader(EXPIRES, now + DURATION_IN_MS);
         } 
         
         chain.doFilter(req, res);
   }
   
  public void init(FilterConfig config) throws ServletException {
        Lifetime = config.getInitParameter(KEY);
     }
  
     public void destroy() {
        Lifetime = null;
     }
}


You can go developer tools commonly available in any almost any browser to check that whether particular resource has Expires or cache control headers set. Learn more on how to speed up your website.

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. my project structure:

    WebContent
    css
    style.css
    js
    lab.js
    images
    logo.png
    WEB-INF
    home.jpg

    I want to set header cache expiration for all content of css, js and images.

    ReplyDelete