J2EE Cache Filter is a Servlet Filter that allows you to set HTTP headers in order to enable browser caching.
Introduction
From Best Practices for Speeding Up Your Web Site:
Add an Expires or a Cache-Control Header
Web page designs are getting richer and richer, which means more scripts, stylesheets, images, and Flash in the page. A first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should be used on all components including scripts, stylesheets, and Flash components.
Configuration
Add the Maven dependency to your pom.xml file:
<dependency>
<groupId>com.samaxes.cachefilter</groupId>
<artifactId>cachefilter</artifactId>
<version>1.0.2</version>
</dependency>Add the filter configuration to your web.xml file:
<filter>
<filter-name>imagesCache</filter-name>
<filter-class>com.samaxes.cachefilter.presentation.CacheFilter</filter-class>
<init-param>
<param-name>privacy</param-name>
<param-value>public</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.cachefilter.presentation.CacheFilter</filter-class>
<init-param>
<param-name>privacy</param-name>
<param-value>public</param-value>
</init-param>
<init-param>
<param-name>expirationTime</param-name>
<param-value>604800</param-value>
</init-param>
</filter>
<filter>
<filter-name>javascriptCache</filter-name>
<filter-class>com.samaxes.cachefilter.presentation.CacheFilter</filter-class>
<init-param>
<param-name>privacy</param-name>
<param-value>private</param-value>
</init-param>
<init-param>
<param-name>expirationTime</param-name>
<param-value>172801</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>imagesCache</filter-name>
<url-pattern>*.png</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>imagesCache</filter-name>
<url-pattern>*.jpg</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>imagesCache</filter-name>
<url-pattern>*.gif</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>cssCache</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>javascriptCache</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>Repository
If you want to use the latest version of J2EE Cache Filter, you need to add the following to your pom.xml:
<project>
...
<repositories>
...
<repository>
<id>googlecode-releases</id>
<name>Google Code Maven repository of releases</name>
<url>http://maven-samaxes-repo.googlecode.com/svn/releases</url>
</repository>
...
</repositories>
...
</project>Note: If you still use the version 1.0.1 of J2EE Cache Filter, you will need to copy the following library files into your classpath:
- commons-lang.jar (2.4) - Commons Lang provides a host of helper utilities for the java.lang API.
- slf4j-log4j12.jar and slf4j-api.jar (1.5.5) - The Simple Logging Facade for Java or (SLF4J) is intended to serve as a simple facade for various logging APIs allowing to the end-user to plug in the desired implementation at deployment time.