tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Keith Bottner" <>
Subject RE: Reducing network traffic for rollover images
Date Sun, 03 Oct 2004 12:09:28 GMT
I am going to have to disagree with the group. I had the same problem and
solved it by creating a Filter that sets the appropriate response header
information. I have included the configuration and the code here.

web.xml entries that are necessary:


Here are the resources that the filter is applied to, of course change them
to what you specifically need.


And here is the filter code:

package filters;

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;

 * This filter inserts the information in its <code>FilterConfig</code>
 * file into the response headers if the resource requested meets the
 * @author Keith Jul 29, 2004
public class ResponseHeaderFilter implements Filter
  FilterConfig fc;
  public void destroy()
    this.fc = null;
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain
chain) throws IOException, ServletException
    HttpServletResponse response = (HttpServletResponse) res;
    // set the provided HTTP response parameters
    Enumeration e = fc.getInitParameterNames();
    while ( e.hasMoreElements() )
      String headerName = (String)e.nextElement();
//      response.addHeader(headerName, fc.getInitParameter(headerName));
      response.setHeader(headerName, fc.getInitParameter(headerName));
    // pass the request/response on
    chain.doFilter(req, res);
  } // doFilter
  public void init(FilterConfig filterConfig)
    this.fc = filterConfig;

This solved the same problem I was having and works for all browsers. Keep
in mind serveral things. One, when doing development you may need to flush
your browser cache if you are updating the images and re-running IE to check
them out as they are cached. Two, once optimized I would suggest changing
the max-age setting in the web.xml to something that is quite longer. Three,
remember to add the appropriate file types as a filter-mapping otherwise you
will pull your hair out wondering why it is not working when in fact it is
just a crappy configuration problem. Four, this is a straight Tomcat
solution, I do not use Apache as my front-end so if you are using Apache as
your front end you will probably need a different solution, although I have
heard, although never verified, that Apache will do the right thing when
serving up static resources and that this is only needed for those of us
running Tomcat standalone.

Of course the great thing about this solution is that it is part of your
jar, no outside server.xml configuration necessary so it can be reused from
application to application. Enjoy, if you have any problems with it let me

Sorry to be the dissenter,

-----Original Message-----
From: Robert Hunt [] 
Sent: Friday, October 01, 2004 12:44 PM
Subject: Reducing network traffic for rollover images

Is there a way (as in, response header or other HTTPServletResponse setting)

that can persuade a
browser to use a cached version of an GIF/JPG that's used for a rollover 
I'd like to reduce the network traffic (and perhaps improve the browser's 
from browsers that attempt to (re)load a rollover image for every tiny 
mousemove increment --
only to receive a 304 response anyway.

-- RH 

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message