the corner office

a blog, by Colin Pretorius

getLastModified

Having posted photies the last few days, I realised that the way I handled file serving, meant the app was re-serving the files each time, which clearly wasn't optimal, nooosirree. This prompted a tweak to the app and a visit to HttpServlet's getLastModified() method. I don't think this method gets used too often in web apps because there's so little static content in 'em, but it's exactly what's needed when you want to send an HTTP 304 / Not Modified response to a client who's sent you a If-modified-since header. I'd never come across the method before, hadn't even realised that servlets could handle this so easily.

You just override getLastModified() (which gets called before service()) and return the resource's age if you have it, and the web container takes care of the comparison with the client's header, and sending the proper response back. If the resource hasn't changed, service() doesn't get called. Nifty.

Now to be virtuous I ought to getLastModified-rinse some of the other static resources. I could go ahead and even handle this for plinks and summary pages, since I know when last anything got updated, and for a quiet site like mine, it might be a good idea. I think I might be getting a bit carried away if I did that, though.

{2007.02.26 00:07}

« I wasn't joking...

» A quick one