aspose file tools*
The moose likes Servlets and the fly likes Stop caching of an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Stop caching of an image" Watch "Stop caching of an image" New topic
Author

Stop caching of an image

Harry Brearley
Greenhorn

Joined: Feb 15, 2006
Posts: 15
Hi
I need to stop caching of an image in a page. I generate a new image each time the page is viewed, by calling a servlet as the source of the <img tag.
The rest of the page is OK to be cached for longer.
Is this possible? I have tried setting the following in the servlet:
response.addHeader("Pragma", "NoCache");
response.addHeader("Cache-Control", "no-cache");
response.addDateHeader("Expires", 1);

Seems to have zero effect. I am testing using latest firefox browser, locally on my m/c so i can guarantee no proxy caching inbetween server and client.

thank you for any help
Harry
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

What I do is to override the getLastModified(request) method in the servlet that returns the image. In your case you could do something simple like returning System.currentTimeMillis().
Harry Brearley
Greenhorn

Joined: Feb 15, 2006
Posts: 15
thanks for the really fast response. I have tried this but the behaviour does not seem to have changed. Do you have this working insider an img statement. Here is mine:

<img src="${context}/sp?spId=${spId}" alt="" />

Above is from my .jsp file

/sp maps to my servlet in web.xml

Perhaps i am missing something else?
Harry Brearley
Greenhorn

Joined: Feb 15, 2006
Posts: 15
I also tried putting the
<%
response.addHeader("Pragma", "NoCache");
response.addHeader("Cache-Control", "no-cache");
response.addDateHeader("Expires", 1);
%>
at the top of the jsp page and keeping the overridden getLastModified(request) method in the servlet.

Same behaviour

I must be missing something?
Rusty Smythe
Ranch Hand

Joined: Aug 09, 2006
Posts: 93

You might try the items referenced in the JSP FAQ: NoCacheHeaders.
[ August 11, 2006: Message edited by: Rusty Smythe ]
Harry Brearley
Greenhorn

Joined: Feb 15, 2006
Posts: 15
i am starting to think there is not an answer for this. It seems when i hit the back button in the browser (testing with firefox) there is no communication with the web server. When i hit the forward button the same.
When i click on a link (like a hit) then it seems to fetch
?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

I don't particularly see what's wrong with allowing the back and forwards buttons to use data from the cache, at least in GET mode. It's quite possible that whatever HTTP spec Firefox is implementing permits that. But I would expect that clicking on a link should work correctly, and it sounds like you are saying it does work correctly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stop caching of an image