Cache problems with h:graphicImage

Hi,

I use a website to manage pictures. The user can choose between 5 picture (in edit mode) and after he submit the new picture it should be shown on the picture.jsp. But if I reload the page after the submit (in session or request Scope) I got the old, cached picture. Is there a way to force a "browser-reload" of my images or the whole picture.jsp to get the correct image?

I draw my images with h:graphicImage

Thnx

Alex

[455 byte] By [Troilusa] at [2007-11-27 6:42:48]
# 1

This may be dependent on what application server you are using, and if you have a web server in front of it and a caching service in front of that.

I think the most portable way to do it is to set the proper HTTP header when the image is returned by the application server. I don't know if there is a standard JEE way to do that beyond writing a servlet that handles your images.

RaymondDeCampoa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

First doublecheck if the image is actually renewed at the server and that the new image is retrieved *after* writing the new image to the disk.

If anything at the serverside seems OK, then this is just a clientside caching issue. Add the following lines to the HTML head to disable clientside caching:

<meta http-equiv="cache-control" content="max-age=0, must-revalidate, no-cache, no-store, private">

<meta http-equiv="expires" content="-1">

<meta http-equiv="pragma" content="no-cache">

BalusCa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> First doublecheck if the image is actually renewed at

> the server and that the new image is retrieved

> *after* writing the new image to the disk.

>

> If anything at the serverside seems OK, then this is

> just a clientside caching issue. Add the following

> lines to the HTML head to disable clientside

> caching:

>

> <meta http-equiv="cache-control"

> content="max-age=0, must-revalidate, no-cache,

> no-store, private">

> <meta http-equiv="expires" content="-1">

> <meta http-equiv="pragma" content="no-cache">

BalusC, won't these headers apply to the request that returns the HTML, not the subsequent request that returns the image?

RaymondDeCampoa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
He was talking about a jsp file, not about an ImageServlet or so.If you're using an ImageServlet, then just add those headers to the HttpServletResponse.
BalusCa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> He was talking about a jsp file, not about an

> ImageServlet or so.

No, he was talking about an image, not a JSP file, not an ImageServlet. He has an image imbedded in an HTML page with an img tag, which is rendered via JSP. At least that is how I read it. He changes the image file, now the browser and any intermediate caches need to know that they have stale data.

>

> If you're using an ImageServlet, then just add those

> headers to the HttpServletResponse.

RaymondDeCampoa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
The caching of the image is definied in the headers. If the image is part of an JSP page, then you have to define the caching in the HTML head of the JSP page.
BalusCa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Images are not part of pages. When you include an img tag in an HTML page, the browser makes a separate HTTP request to retrieve the image. This HTTP request/response will have its own headers, separate from the headers defined for the HTTP request/response that retrieved the HTML page. So setting headers on a JSP page affects only the response that answers with the HTML page and does not affect the HTTP response for the image.

RaymondDeCampoa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
True. But a well-implemented user agent should use the headers of the parent request.
BalusCa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

> True. But a well-implemented user agent should use

> the headers of the parent request.

I'm not sure I agree with that. Most images are static, even when used on pages that change frequently. Admittedly, I'm not an expert on HTTP but I couldn't find anything in RFC 2616 or in the HTML 4.01 specification to support your claim. Do you have a reference I could check?

RaymondDeCampoa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
I am not going to take the time to lookup the exact quote from spec for you. I just tell from my own experience :)
BalusCa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
HI,thanks for all responses. I could solve the problem with adding a fakeId to the picture (pic.jpg?id=Math.random). Its not the best way and sometimes the cache don't reload the picture, but its the best working solution at this moment.ThnxAlex
Troilusa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
I thought of another approach. You could create a Filter for requests for images that should not be cached and add the HTTP header during the filter processing. If you use a consistent naming this would not impact the regular static images.
RaymondDeCampoa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
I think I will try this later when I refactor the image "thing" again. Its a good idea :)CuAlex
Troilusa at 2007-7-12 18:13:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...