0

Am trying to output an image when a Java Servlet link is called. Currently using the following:

response.addHeader("Cache-Control", "max-age=" + CACHE_INTERVAL);
response.addDateHeader("Expires", System.currentTimeMillis() + (1000*CACHE_INTERVAL));

But this still causes the images to reload from the server at times. In comparison if you look at the tags of this:

http://graph.facebook.com/502547234/picture

It never reloads again in Firefox atleast. Any suggestions? What am I missing?

Updated headers:

Mine: https://i.stack.imgur.com/OCesk.png

FB: https://i.stack.imgur.com/7ryRQ.png

Alec Smart
  • 94,115
  • 39
  • 120
  • 184
  • Does 'at times' mean that it reloads 'sometimes' only or always? – home Sep 01 '11 at 09:27
  • Sometimes but am not talking about a hard-refresh. I realize that Facebook's link throws a 304 not modified header where as mine is always 200 OK – Alec Smart Sep 01 '11 at 09:29
  • 1
    this seems to be a duplicate of http://stackoverflow.com/questions/2872613/caching-images-served-by-servlet – Ashkan Aryan Sep 01 '11 at 09:37
  • Can you post Request- and Response-Headers? Once for each: Yours cached and Facebook cached. – Fabian Barney Sep 01 '11 at 10:03
  • In your screenshot the status code is missing, but I think it is 200 since Content-Length is also that high. Updated my answer how to set status code in your response. Do not send any data when you respond with 304! Just the header! Btw: There is a Last-Modified Header field in your response! Changed your code? – Fabian Barney Sep 02 '11 at 10:29
  • @Fatal Yes, I added that after copying it from FB :) – Alec Smart Sep 02 '11 at 12:54
  • 1
    @Alec: check my answer in the duplicate link of Ashkan. You need to take a lot more into account. Your servlet needs to support HEAD requests as well and respond accordingly on `If-Modified-Since` and `If-Match` requests. – BalusC Sep 02 '11 at 12:56

1 Answers1

1

Comparing the headers you're missing Last-Modified field. Setting this field in your response will most likely result the browser request the image next time with a If-Modified-Since request header field.

If not changed you can respond with 304 Not Modified then.

Alternativly you can work with E-Tag instead of using Last-Modified headers when it better fits your needs.


But I am wondering about that your image is not being cached the way you do it. As far as I know you're doing the most agressive caching resulting most browsers DO NOT EVEN ASK FOR NEWER VERSION until expires resulting in completly NO request at all. This is the most agressive caching possible.

One reason for it being not cached may be the URL you call your servlet. What does it look like?

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
  • I've updated my post with the headers. Question, how do I return a 304 not modified? – Alec Smart Sep 02 '11 at 08:06
  • You can set it with `resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED)` where resp is an instance of `HttpServletResponse`. Hope it helps. – Fabian Barney Sep 02 '11 at 10:27
  • How do I know when to return SC_NOT_MODIFIED? I mean atleast once I need to return status 200 with the actual code right. How do I check If-Modified-Since tag is set? Can you please guide me with the code? – Alec Smart Sep 02 '11 at 12:57
  • I was not asking you to do my work. I couldn't find any comprehensive docs about the header tags, so I was asking for help. Am not sure when to return the 304 not modified header and when to return 200. Either ways I'll figure it out. No need to be rude. – Alec Smart Sep 02 '11 at 13:17
  • The problem is that you don't show any effort solving it on your own and asking questions that can be answered with googling for a few minutes - or just by following given links here. – Fabian Barney Sep 02 '11 at 13:26
  • Please don't help if you don't want to. No one has forced you to. I think it is a little premature for you to pass judgement on my efforts. Thank you for your efforts though. I used BalusC's code to complete it. – Alec Smart Sep 02 '11 at 13:35