0

I have a JSP page with following structure:

<input type='file' id='logo' />
<iframe src='view?media=logo'></iframe>

what I am trying to do here is to have user select an image and then I will upload it to a directory inside "WEB-INF" on the server. I have successfully implemented the server side of this meaning that I can see the new image copied to the destination folder. however I need to be able to show the image to the client. Since the image is in "WEB-INF" folder i can not do it by using an 'img' element and manipulating it's 'src' attribute. So I have implemented another servlet with URL pattern 'view'. This servlet receives the media which client is asking for (in this case 'logo') and use following code to forward to page to requested resource:

String media = request.getParameter("media");    
String location = FileManager.getView(media);
getServletContext().getRequestDispatcher(location).forward(request, response);

So if everything goes as planned the recently uploaded image should be shown inside the iframe.

The problem is that ifram keeps showing the old images associated with my request but when i right click on the image and choose 'open in a new window' I see the corrected image.

I did some research and found out that this problem is somehow related to browser chaching the images and the solution suggested wast to add a random number at the end of 'src' attribute:

<img src='logo.png?random=klafjeiej3qk213 />

Though I can not apply this fix to my problem because images are stored inside WEB-INF and I use a servlet to retrieve them.

Is it possible to solve this caching problem (If it is a caching problem) ?

mdoust
  • 429
  • 4
  • 17

2 Answers2

0

I think you can very well apply the mentioned fix. The random number you add is just an HTTP attribute that never gets read. The name of the file stays the same, so you should be ok.

See the existing thread Disable cache for some images

Also see the second solution from the same link, mentioning

header("Pragma-directive: no-cache");
header("Cache-directive: no-cache");
header("Cache-control: no-cache");
header("Pragma: no-cache");
header("Expires: 0");
Community
  • 1
  • 1
sashok_bg
  • 1,739
  • 1
  • 15
  • 27
  • I can not apply the fix because the image element is not inside the jsp page, the servlet with address 'view' will forward an incoming request to the image. the request is sent through the iframe element and the result will be shown inside iframe. As for you second solution, I have already tried setting these header inside my servlet but it has not solved the problem. – mdoust Jul 24 '13 at 15:37
0

Based sashok_bg suggestion I tried to apply this widely suggested fix again. As I've said in the original question images were stored inside WEB-INF directory which are not accessible through JSP page with '' tag. First I tried to apply the fix in the servlet by manipulating the location which request Dispatcher was trying to forward the request. It didn't work...

Then I looked inside the JSP page source in the run-time and what i found out was this:

<img style="-webkit-user-select: none" src="http://localhost:8084/simah/view?media=logo">

This tag was placed inside the element along some other elements. This led me to applying the fix to iframe source itself. after a through observation, it seems to have solved the problem.

<iframe src="<%="view?media=logo&time="+Calendar.getInstance().getTimeInMillis()%>" ></iframe>
mdoust
  • 429
  • 4
  • 17