I have used the following code to create a file in a temp location and save an image in that location, when this page is loaded i always see a red cross ” if the older image is deleted or always shows the cached image not the latest image” .
String file_suffix=df.format(new Date());
File file= new File("/home/martini/Apache/tomcat/apache-tomcat-5.5.27/webapps/dashboard/tmp_imgs/tmp"+file_suffix +".jpeg");
if(!(file.exists())){
file.createNewFile();
}
p_resp.setContentType("image/jpeg");
chart = u.genarateLineChart(m_martiniInstance);
ChartUtilities.saveChartAsJPEG(file, chart, 625, 800);
HTMLHelper.writeHTMLHead(m_out);
m_out.println("<body bgcolor=\"#B4A383\"> "
+ " <center> "
+ " <img src=\"/dashboard/_imgs/sungard_martini.png\" alt=\"SunGard Martini\" border=\"0\" /> "
+ " <p><br><br></p>");
m_out.println(
" <div align=center>"
+ " <img src=\"/dashboard/tmp_imgs/tmp" + file_suffix + ".jpeg\" border=\"0\" /> "
+"</div>"
+ " <p><br><br></p>");
I would like a new file to be created for the image every time and load the new image in a temp file irrespective of a previous image in a temp file being present.
Is there any way of achiveing this because when i see a red cross or a older file i refresh the page and then i see the new image being loaded.
Thanks,
Bhavya
Try to call
response.setHeader("Cache-Control","no-cache");in the beginning of your servlet’s doPost()/doGet() method. This should prevent caching.Other trick is to add some kind of dummy parameter at the end of URL. This can be implemented using javascript at client side. For example your url looks like:
http://myserver/myapp/images/myimage.jpg
change it to
http://myserver/myapp/images/myimage.jpg?dummy=123456
The value of dummy parameter may be timestamp in milliseconds or random number etc.