Download the Cache filter here: Cache Filter Jar Zip

This cache filter is released under an open source Apache License 2.0. The JAR file comes packaged up with the source files. Additional documentation may be found at the cache-filter project.

Deploying the Cache Filter

To deploy the cache filter, download the JAR file, which you can find in the Introduction section of this article. Then:

  • Stop your Tomcat server
  • Copy the cachefilter.jar file to [TOMCAT_HOME]/webapps/ROOT/WEB-INF/lib
  • Modify the [TOMCAT_HOME]/webapps/ROOT/WEB-INF/web.xml file

    The purpose of modifying this file is to map the file types that the cache filter will be processing. We do this by defining the URL pattern we’re looking for (or just the file extension, as in the example below).

    Add this section anywhere you want within the web.xml file.

    <filter>
        <filter-name>imagesCache</filter-name>
        <filter-class>com.samaxes.filter.CacheFilter</filter-class>
        <init-param>
            <param-name>static</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>expirationTime</param-name>
            <param-value>2592000</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>cssCache</filter-name>
        <filter-class>com.samaxes.filter.CacheFilter</filter-class>
        <init-param>
            <param-name>expirationTime</param-name>
            <param-value>604800</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>jsCache</filter-name>
        <filter-class>com.samaxes.filter.CacheFilter</filter-class>
        <init-param>
            <param-name>private</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>expirationTime</param-name>
            <param-value>216000</param-value>
        </init-param>
    </filter>

    This sets up the different filters, and gives them expiration values that get added to the time when the server request was made. The values are in seconds; 216000 corresponds to two and a half days, 604800 corresponds to seven full days, and 2592000 corresponds to 30 full days.

    The next item is to add the filter mapping to this same file. In the example below, we look for specific file extensions; you are not limited to the ones we added there. Add this section to another part of the same file:

    <filter-mapping>
        <filter-name>cssCache</filter-name>
        <url-pattern>*.css</url-pattern>
    </filter-mapping>
     
    <filter-mapping>
        <filter-name>jsCache</filter-name>
        <url-pattern>*.js</url-pattern>
    </filter-mapping>
     
    <filter-mapping>
        <filter-name>imagesCache</filter-name>
        <url-pattern>*.png</url-pattern>
        <url-pattern>*.gif</url-pattern>
        <url-pattern>*.jpg</url-pattern>
    </filter-mapping>
  • Restart the Tomcat server
  • Test!
Posted by:.

2 replies on “Trick for better performance: “Setting up a cache filter in Tomcat for static files”

  1. Hi,

    I am new in jsp servlet and I have implemented the same for Leverage Browser Caching and configured the all extensions in web.xml and it working fine for local host “http://localhost:8080/projname/css/style.css” and server request through IP “http://server.ip.address:8080/projname/css/style.css but this is not working when request is from domain “http://www.domain.com/css/style.css”.

    Please help me to resolve this.

    Server: Tomcat 6.0.44
    Servlet: 2.5

    Thanks

    Like

    1. Probably you are using a proxy (like Nginx or Apache) in front of the Tomcat. And probably your proxy is doing the cache filtering. Review the config of the proxy to know the problem.

      Best regards

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s