Reputation: 922
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
Reputation: 23
Below are steps to implement Caching on Tomcat server
You will have to download "Cache Filter" jar file from the below location. http://code.google.com/p/cache-filter/downloads/list
Put that jar file in tomcat/webapps/ROOT/WEB-INF/lib location.
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>
Restart Tomcat and check the expires headers on browsers.
Reference: http://www.knowarth.com/blog/-/blogs/caching-tomcat-server
Upvotes: 4