Reputation: 2291
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
Upvotes: 0
Views: 564
Reputation: 32953
Another way of tackling what I am guessing is your goal, ie a guaranteed fresh graph on every reload, might be the following approach:
The image servlet should look more or less like
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
PrintWriter out = response.getWriter();
chart = u.genarateLineChart(m_martiniInstance);
ChartUtilities.writeChartAsJPEG(out, chart, 625, 800);
out.close();
}
Not 100% sure of the writeChartAsJPEG call's parameters....
Of course, with the proposed url-pattern the servlet will be called to handle everything in /dashboard/tmp_imgs/tmp/
so if you're using that path for other, physical files you should change it (in web.xml and in the calling code) to something that's unique. It doesn't need to be backed by existing files.
Advantages?
Upvotes: 0
Reputation: 115338
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.
Upvotes: 1