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:

<filter>
  <filter-name>CacheControlHeaderFilter</filter-name>
  <filter-class>filters.ResponseHeaderFilter</filter-class>
  <init-param>
    <param-name>Cache-Control</param-name>
    <param-value>private,max-age=3600</param-value>
  </init-param>
  <init-param>
    <param-name>Pragma</param-name>
    <param-value>cache</param-value>
  </init-param>
</filter>


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

<filter-mapping>
  <filter-name>CacheControlHeaderFilter</filter-name>
  <url-pattern>*.gif</url-pattern>
</filter-mapping>       
<filter-mapping>
  <filter-name>CacheControlHeaderFilter</filter-name>
  <url-pattern>*.css</url-pattern>
</filter-mapping>       
<filter-mapping>
  <filter-name>CacheControlHeaderFilter</filter-name>
  <url-pattern>*.js</url-pattern>
</filter-mapping>       
<filter-mapping>
  <filter-name>CacheControlHeaderFilter</filter-name>
  <url-pattern>*.htc</url-pattern>
</filter-mapping>       
<filter-mapping>
  <filter-name>CacheControlHeaderFilter</filter-name>
 <url-pattern>*.wav</url-pattern>
</filter-mapping>       
<filter-mapping>
  <filter-name>CacheControlHeaderFilter</filter-name>
 <url-pattern>*.swf</url-pattern>
</filter-mapping>       


And here is the filter code:

package filters;

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;


/**
 * This filter inserts the information in its <code>FilterConfig</code>
configuration
 * file into the response headers if the resource requested meets the
filter-mapping.
 * 
 * @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();
// DO NOT USE addHeader it WILL NOT WORK PROPERLY!
//      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
know.

Sorry to be the dissenter,

Keith
-----Original Message-----
From: Robert Hunt [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 01, 2004 12:44 PM
To: [EMAIL PROTECTED]
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 
effect?
I'd like to reduce the network traffic (and perhaps improve the browser's 
"response")
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to