KNOWARTH
KNOWARTH

Reputation: 922

Caching in Tomcat Server

How do I enable caching in Tomcat server so static files can be served from cache for browsers.

I tried with web.xml changes but not sure what code needs to be put in.

Can someone please help on same.?

Upvotes: 2

Views: 1098

Answers (1)

Mittal Mehta
Mittal Mehta

Reputation: 23

Below are steps to implement Caching on Tomcat server

  1. You will have to download "Cache Filter" jar file from the below location. http://code.google.com/p/cache-filter/downloads/list

  2. Put that jar file in tomcat/webapps/ROOT/WEB-INF/lib location.

  3. Edit tomcat/webapps/ROOT/WEB-INF/web.xml file and add filter and filter-mapping properties as mentioned below.

    <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>
    <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>
    
  4. Restart Tomcat and check the expires headers on browsers.

Reference: http://www.knowarth.com/blog/-/blogs/caching-tomcat-server

Upvotes: 4

Related Questions