title: Tomcat 7 returns "304 Not Modified" on modified/non existent resource
body: <p>I am developing a web application that runs on Struts2 and Tomcat 7.0.42.<br>
  I wrote an action that dinamically generates a jsp file, <em>generated.jsp</em>, with some image references in it and creates the associated "_files" folder containing the images. </p>
  
  <p>The action is called in another page, <em>request.html</em>, via JQuery ajax request and then loads the <em>generated.jsp</em> with a JQuery load. The <em>request.html</em> file looks like this:</p>
  
  <pre><code>&lt;HTML&gt;
     &lt;HEAD&gt;
  
         &lt;script language="javascript"&gt;
  
             $(function() {
                 $.ajax({
                    type: "POST",
                     url: "http://..../myAction.action", 
                     data: someDataObj,
                     success: function(msg){
                        $("#myDiv").load("http://.../generated.jsp");
                     });
             }
  
          &lt;/script&gt;
       &lt;/HEAD&gt;
  
      &lt;BODY&gt;
         &lt;div id="myDiv"&gt; &lt;/div&gt;
      &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p>At every request the action first deletes the old files and then generates everything. The generated images depends on the data passed in the ajax request.<br>
  The <em>generated.jsp</em> page contains some directives to make tomcat send response header to prevent the page from being cached:</p>
  
  <pre><code>&lt;% 
  response.setHeader( "Pragma", "no-cache" ); 
  response.setHeader( "Cache-Control", "no-cache" ); 
  response.setDateHeader( "Expires", 0 ); 
  %&gt;
  </code></pre>
  
  <p>If I do open <em>request.html</em> and I do requests not too often, waiting some seconds after the <em>generated</em> page is displayed, it all works as expected: the page and its images are displayed.</p>
  
  <p>But if I do a couple of requests very fast, like waiting less than a second for each one, I see that the images displayed are the ones generated from the older request.  </p>
  
  <p>Looking at the request and response headers, when it works good the response code for the page and images is always "<strong>200 OK</strong>".<br>
  When the problem occours, tomcat answers the GET request for <em>generated.jsp</em> page with status code "<strong>200 OK</strong>", but the requests for the images it returns "<strong>304 Not Modified</strong>" even if the image files are different from the last request.</p>
  
  <p>There are the request/response headers:</p>
  
  <blockquote>
  <pre><code>Request URL:http://.../generated.jsp
  Request Method:GET
  Status Code:200 OK
  Request Headersview source
  Accept:text/html, */*; q=0.01
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:en-US,en;q=0.8,it;q=0.6
  Connection:keep-alive
  Cookie:JSESSIONID=44E591CE76423F14CCFBE2DF86F50DDE
  Host:127.0.0.1:8080
  User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
  X-Requested-With:XMLHttpRequest
  Response Headersview source
  Cache-Control:no-cache
  Content-Type:text/html;charset=ISO-8859-1
  Date:Tue, 03 Dec 2013 07:54:05 GMT
  Expires:Thu, 01 Jan 1970 00:00:00 GMT
  Pragma:no-cache
  Server:Apache-Coyote/1.1
  Transfer-Encoding:chunked
  </code></pre>
  </blockquote>
  
  <p>&nbsp;</p>
  
  <blockquote>
  <pre><code>Request URL:http://.../generated.jsp_files/img.png
  Request Method:GET
  Status Code:304 Not Modified
  Request Headersview source
  Accept:image/webp,*/*;q=0.8
  Accept-Encoding:gzip,deflate,sdch
  Accept-Language:en-US,en;q=0.8,it;q=0.6
  Cache-Control:max-age=0
  Connection:keep-alive
  Cookie:JSESSIONID=300B5CB436CD60BA8E129B21B085A965
  Host:127.0.0.1:8080
  If-Modified-Since:Mon, 02 Dec 2013 15:31:57 GMT
  If-None-Match:W/"1481-1385998317000"
  User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
  Response Headersview source
  Date:Mon, 02 Dec 2013 15:32:00 GMT
  ETag:W/"1481-1385998317000"
  Server:Apache-Coyote/1.1
  </code></pre>
  </blockquote>
  
  <p>I don't think that the problem is caused by the browser cache, because the requests are done and the browser uses the cache only after it receives the 304 status code.  </p>
  
  <p>I have tried different things, until I found a workaround.<br>
  First, I've modified the action to delete the old files at every request, page and images, and then generate everything, but with no success.  </p>
  
  <p>Then I've tried to generate the images every time with a different name, concatenating a timestamp to the file name.<br>
  What happens is that <em>generated.jsp</em> is first deleted, then generated with the reference to the new image names at every request.<br>
  In this case when the browser requests <em>generated.jsp</em> tomcat returns the page with <strong>200 OK</strong> status code. When the problem occours, looking at the source of the page, I can see that what is returned is the page with the <strong>old</strong> image name references.<br>
  It seemed like tomcat don't recognize the file changes and that the old images doesn't exist anymore, because at the requests of the image with the old name it still answers with a <strong>304</strong> status code.  </p>
  
  <p>Seeing this behaviour, I checked tomcat's work directory to find the "java" and "class" files corresponding to <em>generated.jsp</em> and I found that those files has a timestamp older that the page contained in the webapp folder.</p>
  
  <p>At this point I've tried another workaround: make the action delete tomcat's work subfolder containing <em>generated.jsp</em> related files.<br>
  Even this attempt failed, same behaviour with the browser receiving the old <em>generated.jsp</em> page and the 304 status code for images.</p>
  
  <p>The only workaround that did the trick was to delete the old page in webapp folder and create the page with a different name, like I did with the images.</p>
  
  <p>I read in the documentation that Tomcat does check at every request if a resource is modified, unless some properties in <em>web.xml</em> are changed.<br>
  Seeing this behaviour I believe that tomcat uses some cache to do the check and doesn't do the that on the filesystem every time, but I haven't found anything in the documentation.</p>
  
  <p>Does someone knows if I am wrong and maybe help me understand how those mechanism works in Tomcat?</p>