Reputation: 411
I would like to display a graphic image in a jsf web application from a xhtml file that's changing for each run. I am displaying an image once the upload is done. But what happening is after uploading any image it always displays the one which I uploaded at first time. On doing refresh it displaying the recent uploaded image. Why it is always displaying an image I uploaded at first try. How could I display the most recent uploaded image with h:graphicImage
. I am using servlet to display an image. Can anyone help me out...???
here is my code for displaying an image:
public void displayImage(byte[] image,HttpServletResponse response) {
ServletOutputStream out;
if ( image != null ) {
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(new ByteArrayInputStream(image));
out = response.getOutputStream();
ImageIO.write(bufferedImage, "png",out);
} catch (IOException e) {
logger.info("Exception in reading the image" + e);
}
} else {
logger.info("No bytes present ");
}
this is my actual requirement... In first window I have signature box. In that I have upload button.Once the upload is done then I will show an window(2) with an uploaded image there i can crop the image such that I could save it on the signature box of the first window. it is working fine. But what happens when i cancel/close the 2nd window is the signature box of 1st remains empty. So user can upload some other image. In that case, when I try to upload an image, Its successfully getting uploaded but it is not displayed on 2nd window it still the shows the image which I uploaded at first try. How do i fix it...???
Upvotes: 2
Views: 5044
Reputation: 1109715
Tell the browser to not cache the image. Set the following headers on the servlet response before the first bit is ever written to its body:
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.
To prevent hard-nosed browsers from still caching it, add a request parameter with a timestampto the servlet URL.
<h:graphicImage value="imageServlet?id=#{bean.imageId}&t=#{now.time}" />
Where #{now}
is been registered in faces-config.xml
as follows:
<managed-bean>
<managed-bean-name>now</managed-bean-name>
<managed-bean-class>java.util.Date</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
Upvotes: 3