2

it appears this is cached:

http://agrozoo.net/UploadedImages/d334e86792a547389580a5b5a6a9dcf2_thumb.jpg

and this not:

http://agrozoo.net/UploadedImages/d334e86792a547389580a5b5a6a9dcf2-Phlebia-livida.jpg

Sample screen shoot from firefox/firebug, left cached, right not cached

in first case I do simple:

chain.doFilter(request, response);

in second:

response.setContentType("image/jpg");
            OutputStream os = response.getOutputStream();
            ImageIO.write(buffer, "jpg", os);
            os.close();

What to do to make it cache in second case ?

Miran C
  • 105
  • 1
  • 10
  • It looks like with your second image, the image gets a new timestamp every time process a request. So apache will not return '304 not modified'. To cache static assets such as images, css and javascript, the usual practice is to serve them directly with apache or nginx, instead of processing those requests with java etc. – Håken Lid Feb 11 '16 at 11:51
  • The content is not static, I'm adding wattermak etc to image before serving to client. – Miran C Feb 12 '16 at 08:50
  • So if it's not static, then it shouldn't be cached. – Håken Lid Feb 12 '16 at 10:26

2 Answers2

0

Both the left and the right have a Cache-Control: max-age=0 header which will disable caching

But the left has an etag which is likely causing the caching

See here to see the headers which can disable caching

Community
  • 1
  • 1
lance-java
  • 25,497
  • 4
  • 59
  • 101
0

Solution:

response.setHeader("ETag", eTag);



if (request.getHeader("If-None-Match") != null && request.getHeader("If-None-Match").equals(eTag))
{        
  response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); // 304
  response.setContentType("image/jpg");
  OutputStream os = response.getOutputStream();
  buffer.flush();//force 0 B jpg image, don't need to send anything as it will be pulled from browser cache
  ImageIO.write(buffer, "jpg", os);
  os.close();
}
else
{    

response.setContentType("image/jpg");
OutputStream os = response.getOutputStream();
ImageIO.write(buffer, "jpg", os);
os.close();
}
Miran C
  • 105
  • 1
  • 10