In my project we use some img tags like that
<img src="url to java servlet?parameters">
while the servlet responds the bytesteam right away, something like
response.getOutputStream().write(imageBytes);
That all works fine, but is there any way of caching that image in the browser, because it wont cache it automatically like regular url images.
You need to set at least the
ETag,Last-ModifiedandExpiresheaders on the response. TheETagheader should represent the unique identifier of the file based on a combination of filename, filesize and lastmodified timestamp). TheLast-Modifiedheader should represent the last modified timestamp of the file. TheExpiresheader should inform the client how long it is allowed to keep the file in cache.If the cache has been expired in the client side and the
ETagorLast-Modifiedare available, then the client will send aHEADrequest to check if the file needs to be renewed. If not, then theExpireswill just be postponed again accordingly.You can find here a servlet example which handles this all (and download resumes and automatic GZIP): FileServlet supporting resume and GZIP.
However, if your files are available on the disk file system already (and not in a database), then you should consider just delegating the job to the servletcontainer’s builtin default servlet. See also Reliable data serving.