Forums Register Login

how to cache an image in browser

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 ]
Crusading Chameleon likes the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6728 times.
Similar Threads
Image related
How can I prevent to store image in browser cache
Java & images
Public and Private Cache
image in JSP page
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:30:05.