Set Expire header for images,css, js files in tomcat server
by janani[ Edit ] 2013-04-10 09:54:09
Web.xml
<filter>
<filter-name>headersFilter</filter-name>
<filter-class>HeaderFilter</filter-class>
<init-param>
<description>Add an Expires Header</description>
<param-name>header</param-name>
<param-value>Expires: Wed, 03 APR 2013 00:00:00 GMT</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>headersFilter</filter-name>
<url-pattern>*.jpg</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>headersFilter</filter-name>
<url-pattern>*.jpeg</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>headersFilter</filter-name>
<url-pattern>*.png</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>headersFilter</filter-name>
<url-pattern>*.gif</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>headersFilter</filter-name>
<url-pattern>*.css</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>headersFilter</filter-name>
<url-pattern>*.js</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
HeaderFilter.java
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.logging.Logger;
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;
public class HeaderFilter implements Filter {
private static final Logger logger = Logger.getLogger(HeaderFilter.class.toString());
private FilterConfig filterConfig;
private Map headersMap;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
String headerParam = filterConfig.getInitParameter("header");
if (headerParam == null) {
logger.info("No headers were found in the web.xml (init-param) for the HeaderFilter !");
return;
}
// Init the header list :
headersMap = new LinkedHashMap();
if (headerParam.contains("|")) {
String[] headers = headerParam.split("|");
for (String header : headers) {
parseHeader(header);
}
} else {
parseHeader(headerParam);
}
logger.info("The following headers were registered in the HeaderFilter :");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (headersMap != null) {
// Add the header to the response
}
// Continue
chain.doFilter(request, response);
}
public void destroy() {
this.filterConfig = null;
this.headersMap = null;
}
private void parseHeader(String header) {
String headerName = header.substring(0, header.indexOf(":"));
if (!headersMap.containsKey(headerName)) {
headersMap.put(headerName, header.substring(header.indexOf(":") + 1));
}
}
}
THavamani
useful information for java developers
0
0
Add Reply