tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Funk <funk...@joedog.org>
Subject Re: Client-side caching via If-Modified-Since
Date Wed, 08 Sep 2004 12:40:32 GMT
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


Mime
View raw message