Note: Tomcat 7 is no longer supported in our latest version 8.x. Please check the link here for similar functionality on Tomcat 9.
Apache Tomcat 9 documentation on the ExpiresFilter
Introduction
In a previous article, I talked about setting up a cache filter in Tomcat 6. The purpose of that filter was to add header information to static files so that they don't expire. This lightens the load on the Tomcat server, since the browser will first check its cache before requesting another static file.
In Tomcat 7, this cache filtering functionality has been added to Tomcat itself, so all we have to do is reference the right classes in the web application's web.xml file.
Note regarding JRS 6.x, Cache Filter is built directly in JRS in recent versions, see http://community.jaspersoft.com/wiki/setting-cache-filter-tomcat-or-jboss-static-files#Cache_Control_filter_in_6.x
Process
Shutdown the Tomcat server
We first need to shutdown Tomcat before making these modifications. Tomcat will not take these modifications into account until the server restarts.
Modify the web application's web.xml file
You'll need to add these snippets to the {TOMCAT_HOME}/webapps/jasperserver-pro/WEB-INF/web.xml, which enable the ExpiresFilter. The filter mapping makes sure that every request gets evaluated as a potential candidate for the cache filter.
The class org.apache.catalina.filters.ExpiresFilter is in the catalina.jar file under {TOMCAT_HOME}/lib.
<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 2 weeks</param-value> </init-param> <init-param> <param-name>ExpiresByType text/css</param-name> <param-value>access plus 2 weeks</param-value> </init-param> <init-param> <param-name>ExpiresByType application/javascript</param-name> <param-value>access plus 2 weeks</param-value> </init-param> </filter> <filter-mapping> <filter-name>ExpiresFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>
Modify the context.xml file
Next, we need to modify the application's context.xml file to allow proxy caching. To do this, we simply add the following snippet below the declaration, where we add the BasicAuthenticator Valve. Also note that at the Context level, the cachingAllowed attribute is set to true.
<context cachingallowed="true" crosscontext="true" debug="5" path="/jasperserver-pro" reloadable="false"> <valve classname="org.apache.catalina.authenticator.BasicAuthenticator" disableproxycaching="false" securepageswithpragma="true"> . . . </valve> </context>
Restart Tomcat and test
Once you've restarted Tomcat, you can start testing that the headers are getting their expiration headers properly set. You can use Firebug to inspect the headers, or you can use Chrome's integrated developer tools.
References:
- Apache documentation on the ExpiresFilter - http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter
- Apache documentation on the BasicAuthenticator Valve - http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Basic_Authenticator_Valve
- Setting up a cache filter in Tomcat 6 - setting up a cache filter in Tomcat 6.
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now