You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Joost Baaij <jo...@spacebabies.nl> on 2004/09/08 14:33:02 UTC

Client-side caching via If-Modified-Since

Hi all, I'm new to the list. Have searched on the web for answers to my
question, but was unable to find much.

Summary: I am trying to utilize client-side caching via
the If-Modified-Since request header and 304 response code for jsp pages.


There is a mechanism in Tomcat that returns status 304 for static files
that have not been modified since the last request by the client. I've
seen it in my logs, but only for gif, jpg, css files and the like.

I run a website with many thousands of articles online. Every time Google
et al come around, my load shoots up since they're requesting all
articles. I added the Last-Modified header to all articles, in the
understanding that Tomcat would match this header with the incoming
If-Modified-Since, and return 304 for old articles.

This does not seem to happen though (Tomcat 4.1.24). Correct?

So then I'm left with writing a filter that does this trick for me.
Unfortunately I'm stuck there too (I've attached it). My question really
is, should this functionality be in Tomcat and if it is, how can I use it?

Joost.

Re: Client-side caching via If-Modified-Since

Posted by Tim Funk <fu...@joedog.org>.
It belongs in a filter like you have done. It doesn't belong in tomcat.

Also your filter isn't correct - it is still executing your JSP code and 
wasting resources. It should be:
doFilter() {
   ...
   if (contentIsFresh) {
     httpResponse.sendError(HttpServletResponse.SC_NOT_MODIFIED);
   } else {
     chain.doFilter( ... );
   }
}

-Tim

Joost Baaij wrote:
> Hi all, I'm new to the list. Have searched on the web for answers to my
> question, but was unable to find much.
> 
> Summary: I am trying to utilize client-side caching via
> the If-Modified-Since request header and 304 response code for jsp pages.
> 
> 
> There is a mechanism in Tomcat that returns status 304 for static files
> that have not been modified since the last request by the client. I've
> seen it in my logs, but only for gif, jpg, css files and the like.
> 
> I run a website with many thousands of articles online. Every time Google
> et al come around, my load shoots up since they're requesting all
> articles. I added the Last-Modified header to all articles, in the
> understanding that Tomcat would match this header with the incoming
> If-Modified-Since, and return 304 for old articles.
> 
> This does not seem to happen though (Tomcat 4.1.24). Correct?
> 
> So then I'm left with writing a filter that does this trick for me.
> Unfortunately I'm stuck there too (I've attached it). My question really
> is, should this functionality be in Tomcat and if it is, how can I use it?
> 
> Joost.
> 
> 
> ------------------------------------------------------------------------
> 
> package nl.gomagazine.web;
> 
> import java.io.IOException;
> import java.util.logging.*;
> 
> 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.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
> 
> /**
>  * @author joost
>  */
> public class CacheFilter implements Filter {
> 	private int responseCode = HttpServletResponse.SC_NOT_MODIFIED;
> 	private String responseMsg = "Not modified";
> 	private static Logger log = null;
> 
> 	/**
> 	 * @see Filter#init(FilterConfig)
> 	 */
> 	public synchronized void init(FilterConfig filterConfig) throws ServletException {
> 		if(log == null) {
> 			log = Logger.getLogger("nl.gomagazine.web");
> 			try {
> 				Handler fh = new FileHandler("/export/web/gomagazine/www/logs/cachefilter.log", true);
> 				fh.setFormatter(new java.util.logging.SimpleFormatter());
> 				log.addHandler(fh);
> 				log.setLevel(Level.ALL);
> 				log.config("Program started");
> 			} catch (IOException e) {
> 				throw new ServletException(e);
> 			} catch (SecurityException e) {
> 				throw new ServletException(e);
> 			}
> 		}
> 	}
> 
> 	/**
> 	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
> 	 */
> 	public void doFilter(ServletRequest request, ServletResponse response,	FilterChain chain) throws IOException, ServletException {
> 		HttpServletRequest httpRequest = (HttpServletRequest)request;
> 		HttpServletResponse httpResponse = (HttpServletResponse)response;
> 
> 		log.info("filter activated for request="+httpRequest.getRequestURL());
> 		// always pass the request/response on
> 		chain.doFilter(request, response);
> 
> 		// if If-Modified-Since was set, modify the response
> 		long ifModifiedSince = httpRequest.getDateHeader("If-Modified-Since");
> 		if(ifModifiedSince > -1) {
> 			log.config("request has If-Modified-Since set");
> 			long lastModified = httpRequest.getDateHeader("Last-Modified");
> 			if(ifModifiedSince > lastModified)
> 				httpResponse.sendError(this.responseCode, this.responseMsg);
> 		}
> 	}
> 
> 	/**
> 	 * @see Filter#destroy()
> 	 */
> 	public void destroy() {
> 
> 	}
> }
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org