Skip to content

Instantly share code, notes, and snippets.

@danlangford
Created September 7, 2012 20:46
Show Gist options
  • Save danlangford/3669475 to your computer and use it in GitHub Desktop.
Save danlangford/3669475 to your computer and use it in GitHub Desktop.
generic java response header filter
package mo.nkeyco.de;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
/* http://www.symphonious.net/2007/06/19/caching-in-tomcat/ */
public class ResponseHeaderFilter implements Filter {
private FilterConfig config;
@Override
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpResp = (HttpServletResponse) response;
Enumeration e = config.getInitParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String value = config.getInitParameter(name);
httpResp.addHeader(name, value);
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
/* im not sure if there is anything we need to do on destroy -dL */
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<!-- some WEB.XML goodness -->
<filter>
<!-- http://www.symphonious.net/2007/06/19/caching-in-tomcat/ -->
<filter-name>NoCache</filter-name>
<filter-class>mo.nkeyco.de.ResponseHeaderFilter</filter-class>
<init-param>
<!-- this is preferred for the 1.1 spec and is used by CLIENT BROWSERS -->
<param-name>Cache-Control</param-name>
<param-value>no-cache, must-revalidate</param-value>
</init-param>
<init-param>
<!-- this is for devices between server and client, like VIP, F5, load-balancers, caches and proxies
also some 1.0 clients respect this -->
<param-name>Pragma</param-name>
<param-value>no-cache</param-value>
</init-param>
<init-param>
<!-- older browsers may not honor the Cache-Control from v1.1, Expires existed in 1.0 spec -->
<param-name>Expires</param-name>
<param-value>0</param-value>
</init-param>
</filter>
<filter>
<filter-name>CacheOneDay</filter-name>
<filter-class>mo.nkeyco.de.ResponseHeaderFilter</filter-class>
<init-param>
<param-name>Cache-Control</param-name>
<param-value>max-age=86400, public</param-value>
<!-- a week would be '604800' -->
</init-param>
<!-- dont know if 'Pragma' has/needs any age optione -->
<!-- to use 'Expires' you would need to tweek the Java to calculate the time for you-->
</filter>
<!-- this will prevent some files from being cached. -->
<filter-mapping>
<filter-name>NoCache</filter-name>
<!-- i also made a CacheOneDay filter you can try, you can tweek it for a CacheOneWeek or whatever you want -->
<url-pattern>/scripts/lib/no-cache/*</url-pattern>
</filter-mapping>
<!-- other WEB.XML goodness -->
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment