bhavs
bhavs

Reputation: 2291

image loaded using a servlet always shows a red cross

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

Answers (2)

fvu
fvu

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:

  1. Create a servlet that handles (url-pattern) /dashboard/tmp_imgs/tmp/* (notice the *).
  2. Have your present code generate the dummy image url as it does now. The exact filename is not important, it's just always different to trick the browser into reloading it always.

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?

  • Each servlet handles 1 task, no implicit multitasking behind your back
  • No file ever gets written to disk, less clutter and improved performance

Upvotes: 0

AlexR
AlexR

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

Related Questions