I've a jsp page which loads many images. I'd like to cache the images for faster loading.
I'll explain my idea, please correct it if it's wrong. I'm calling the picture loading servlet for each image and return as a BLOB. My idea is to add a modified date with the image and the other values like Last-Modified, expires, Cache-control and max age. And thereby make the browser understand if the image changes.
But how can i append a modified date to a BLOB? Or is there some better ideas to make them cachable?
Thanks...
(moved from the duplicate question)
Add a filter (
javax.servlet.Filter
) that adds the cache headers whenever the response contains an image. Something like:(where
DateUtil
isorg.apache.commons.httpclient.util.DateUtil
)The above filter, of course, assumes you have set the right
Content-Type
for your images. Otherwise I think they might not be displayed properly in the browser, with or without cache.To answer your question:
That's a different scenario. You can store your images in some cache implementation (ehcache for example), but this is a server cache, not client cache.
This is a Good ThingTM.
For that you actually need the
ETag
,Last-Modified
and optionally alsoExpires
header. With theETag
header both the server and client can identify the unique file. You can if necessary use under each the database key for this. With theLast-Modified
header header both the server and client knows if they both have the same version of the file. With theExpires
header you can instruct the client when to re-request the file the firstnext time (thus, when the date as specified inExpires
has been expired).The
Cache-Control
header is not so relevant here as you just want to allow caching and the average client already does that by default.For more information and a servlet example, you may find this article useful and maybe also this article for the case you'd be interested in tuning performance of a JSP/Servlet webapplication.
Just add one more column to the database table in question which represents the insertion date. In most DB's you can just use
now()
function for this or even create it as an auto-trigger so that it get set automatically on every insert/update.Don't you want to amend the HTTP headers, rather than the actual image/BLOB itself ?
See this page for info on setting headers in the servlet response. I suspect you'll be looking at standard headers such as
Expires
.I believe you should set the appropriate headers for controlled caching.
You can always use the http protocol specs as a reference for sending and setting the appropriate headers (response headers).
You can have a look at the following links:
Caching in HTTP http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
Http Protocol Header Field Definitions http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
regards,