Tomcat Leverage Browser Caching

Enable tomcat cache filter (ExpiresFilter) in you application server can improve performance by reducing network load for some resources.

ExpiresFilter is a Java Servlet API port of Apache mod_expires. This filter controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. The expiration date can set to be relative to either the time the source file was last modified, or to the time of the client access.

These HTTP headers are an instruction to the client about the document's validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered "expired" and invalid, and a new copy must be obtained from the source.

To modify Cache-Control directives other than max-age, you can use other servlet filters or Apache Httpd mod_headers module

Edit file web.xml, add filter and filter-mapping like this:

        <param-name>ExpiresByType image</param-name>
        <param-value>access plus 1 month</param-value>
        <param-name>ExpiresByType text/css</param-name>
        <param-value>access plus 4 weeks</param-value>
        <param-name>ExpiresByType application/javascript</param-name>
        <param-value>access plus 30 days</param-value>

Restart Tomcat

sudo service tomcat7 restart


Popular posts from this blog