Thursday 17 July 2014

Enable caching in tomcat 7

Caching is technique of storing files for future use. In we take it in context of website performance, caching helps browser to store different resources from server in local storage. Whenever a user request for resource browser first checks it cache and if it is available the resource is served from cache. This reduces network bandwidth and increases performance of your website. ( how to speed up your website ) 

Tomcat 7 has inbuilt cache filtering functionality. It uses ExpiresFilter, that is a Java Servlet API port of Apache mod_expires. This filters sets the expires headers for the HTTP responses. Follow the steps to enable caching on your tomcat 7 server.

Modify your web.xml file by adding below code


<filter>
 <filter-name>ExpiresFilter</filter-name>
 <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
 <init-param>
    <param-name>ExpiresByType image</param-name>
    <param-value>access plus 10 minutes</param-value>
 </init-param>
 <init-param>
    <param-name>ExpiresByType text/css</param-name>
    <param-value>access plus 10 minutes</param-value>
 </init-param>
 <init-param>
    <param-name>ExpiresByType application/javascript</param-name>
    <param-value>access plus 10 minutes</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>ExpiresFilter</filter-name>
 <url-pattern>/*</url-pattern>
 <dispatcher>REQUEST</dispatcher>
</filter-mapping>

You can specify any numeric values in param-values. However you can change the time unit in terms of days, minutes hours etc. Various units can be
  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds
For more reference and detailed description you can Follow link. http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter

No comments:

Post a Comment