aspose file tools*
The moose likes JSP and the fly likes how to  cache an image in browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to  cache an image in browser " Watch "how to  cache an image in browser " New topic
Author

how to cache an image in browser

nikitha kakani
Greenhorn

Joined: Dec 15, 2007
Posts: 25
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
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
nikitha kakani
Greenhorn

Joined: Dec 15, 2007
Posts: 25
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
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
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

Joined: Dec 15, 2007
Posts: 25
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
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to cache an image in browser
 
Similar Threads
How can I prevent to store image in browser cache
Image related
Java & images
image in JSP page
Public and Private Cache