2

I'm using the <enable-file-serving value="true" /> feature of WebSphere Application Server v7.0 to serve static content (images, CSS, JavaScripts) for my Java web app.

How can I modify the HTTP headers for this static content (e.g., add a Cache-Control or Expires header)?

quietmint
  • 13,885
  • 6
  • 48
  • 73
  • Hi, the recommended approach is moving the static content to IHS(IBM HTTP Server - just an apache 2 actually) that would be in front of WAS doind Load Balance and failover if required. Check: http://www-01.ibm.com/software/webservers/httpservers/ – groo Mar 15 '13 at 03:35
  • @MarcosMaia I'm already using IHS, but the difficulty of extracting/distributing/synching static content so its served from the web server instead of the application server isn't worth the headache in my environment. – quietmint Mar 15 '13 at 15:37

1 Answers1

3

I ended up writing a Filter to add the HTTP header based on the URL of the requested resource. Here's a simplified version:

CacheFilter.java

public class CacheFilter implements Filter {
    private static long maxAge = 86400 * 30; // 30 days in seconds

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setHeader("Cache-Control", "max-age=" + maxAge);
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}

web.xml

<filter>
    <filter-name>cache</filter-name>
    <filter-class>com.example.CacheFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cache</filter-name>
    <url-pattern>*.png</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>cache</filter-name>
    <url-pattern>*.jpg</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>cache</filter-name>
    <url-pattern>*.gif</url-pattern>
</filter-mapping>
quietmint
  • 13,885
  • 6
  • 48
  • 73
  • According to the specification (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) max-age is given in seconds not in millis, so it should be private static long maxAge = DateUtils.SECONDS_PER_DAY * 30;(i dont know if that constant exists) otherwise great answer – jambriz Apr 16 '13 at 21:01
  • @jambriz You're right. I'm used to JavaScript where the standard unit is milliseconds. I've corrected the answer. – quietmint Apr 16 '13 at 21:23
  • I am trying to do something similar. But It's not working. I still see no-cache. do you think I am missing anything. I have exactly same code like u. – webdev Oct 10 '13 at 17:05
  • I tried the above filter it was able set max age for the image files in my local host. But, how do I set max age for image files which are being coming from another server? I was making aJAx call to load images from a third party server –  Feb 14 '14 at 22:42
  • @Sarath You can't, unless you proxy those images through your own server and control the headers using something like the above. – quietmint Feb 16 '14 at 03:47