File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to cache an image in browser

 
nikitha kakani
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone ,Can any one tell me how to prevent caching an image in broser .when i first user logs in the out.gif is stored in cache & second user is also getting same image . thanks in advance
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is to append a timestamp or unique ID to the image URL, like "...image.jpg?id=8912763482763487264".

The proper way would be to set correct caching response headers when serving the image, so that the browser knows not to cache it.
 
nikitha kakani
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply ,i tried in my jsp:

<%
response.setHeader( "Pragma", "no-cache" );
response.setHeader( "Cache-Control", "no-cache" );
response.setDateHeader( "Expires", 0 );
%>
<body>....
<img src="images/out.gif" width="75" height="75" ><br>

but the image is not expired .

i also used :
java.util.Date d=new java.util.Date();
long l=d.getTime()l
response.setDateHeader("Expires",l);

As the servlet is forwarding request to jsp ,i am unable to see wheather jsp is expired or not ,but mainly the image is not expired (indicating None)

thanks in advance
nikki
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The headers in the JSP control the caching of the HTML page generated by the JSP, not the caching of any images it references. You need to set those headers for the image. That may mean that you need to address images through a servlet, which streams it to the browser and can set headers at will.
 
nikitha kakani
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you d't mind can you tell me more clearly

i added this in jsp for the sake of images :

response.setHeader("cache-control", "max-age=0, must-revalidate, no-cache, no-store, private"); // HTTP 1.1
response.setHeader("pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("expires", -1); // Stop proxy caching

but no change same image in cache is displayed .Can you send me a piece of code how to address image from servlet .thanks in advance
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing you do in the JSP is going to affect the images - it will affect the caching of the HTML page.

The CodeBarn has an example of how to stream images from a servlet. I'm not sure if it sets headers, have a look at its source. You'll need to pass the name of the image as URL parameter to the servlet, so that the servlet can read the file from disk.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
The CodeBarn has an example of how to stream images from a servlet. I'm not sure if it sets headers, have a look at its source. You'll need to pass the name of the image as URL parameter to the servlet, so that the servlet can read the file from disk.




It does set headers (but not no-cache headers).
It's not necessary to pass the image name as a parameter.
The servlet is mapped to "/images/*" and the servlet parses the image name from the URL.
[ February 18, 2008: Message edited by: Ben Souther ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic