Tomcat 7返回“304 Not Modified”;在已修改/不存在的资源上 [英] Tomcat 7 returns "304 Not Modified" on modified/non existent resource

查看:172
本文介绍了Tomcat 7返回“304 Not Modified”;在已修改/不存在的资源上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在Struts2和Tomcat 7.0.42上运行的Web应用程序。

我写了一个动作,用于生成一个jsp文件, generated.jsp ,其中的图像引用并创建包含图像的关联_files文件夹。

I am developing a web application that runs on Struts2 and Tomcat 7.0.42.
I wrote an action that dinamically generates a jsp file, generated.jsp, with some image references in it and creates the associated "_files" folder containing the images.

通过JQuery ajax请求在另一个页面 request.html 中调用该操作,然后加载 generated.jsp 使用JQuery加载。 request.html 文件如下所示:

The action is called in another page, request.html, via JQuery ajax request and then loads the generated.jsp with a JQuery load. The request.html file looks like this:

<HTML>
   <HEAD>

       <script language="javascript">

           $(function() {
               $.ajax({
                  type: "POST",
                   url: "http://..../myAction.action", 
                   data: someDataObj,
                   success: function(msg){
                      $("#myDiv").load("http://.../generated.jsp");
                   });
           }

        </script>
     </HEAD>

    <BODY>
       <div id="myDiv"> </div>
    </BODY>
</HTML>

每次请求时,操作首先删除旧文件,然后生成所有内容。生成的图像取决于ajax请求中传递的数据。

generated.jsp 页面包含一些指令,使tomcat发送响应头以防止页面被缓存:

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.
The generated.jsp page contains some directives to make tomcat send response header to prevent the page from being cached:

<% 
response.setHeader( "Pragma", "no-cache" ); 
response.setHeader( "Cache-Control", "no-cache" ); 
response.setDateHeader( "Expires", 0 ); 
%>

如果我打开 request.html 并且我不经常请求在显示生成的页面后等待几秒钟,它全部按预期工作:显示页面及其图像。

If I do open request.html and I do requests not too often, waiting some seconds after the generated page is displayed, it all works as expected: the page and its images are displayed.

但如果我快速做几个请求,比如每个请求等待不到一秒,我看到显示的图像是从旧请求生成的图像。

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.

查看请求和响应标头,当它运行良好时,页面和图像的响应代码始终为 200 OK 。< br>
问题发生后,tomcat回复了 generated.jsp 页面的GET请求,其状态代码为 200 OK ,但对图像的请求却是即使图像文件与上一个请求不同,也会返回 304未修改

Looking at the request and response headers, when it works good the response code for the page and images is always "200 OK".
When the problem occours, tomcat answers the GET request for generated.jsp page with status code "200 OK", but the requests for the images it returns "304 Not Modified" even if the image files are different from the last request.

有请求/响应标头:


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


 


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


我认为这个问题不是由浏览器缓存造成的,因为请求已经完成,浏览器只在收到304状态代码后才使用缓存。

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.

我尝试过不同的东西,直到找到解决方法。

首先,我已经修改了每次请求时删除旧文件的操作,页面和图像,然后生成一切,但没有成功。

I have tried different things, until I found a workaround.
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.

然后我尝试使用不同的名称生成图像,将时间戳连接到文件名。

会发生什么首先删除 generated.jsp ,然后在每次请求时使用对新图像名称的引用生成。

在这种情况下,当浏览器请求 generated.jsp tomcat返回带有 200 OK 状态代码的页面。当问题出现时,查看页面的来源,我可以看到返回的是带有图像名称引用的页面。

好​​像是tomcat don' t识别文件更改并且旧图像不再存在,因为在具有旧名称的图像的请求下,它仍然以 304 状态代码回答。

Then I've tried to generate the images every time with a different name, concatenating a timestamp to the file name.
What happens is that generated.jsp is first deleted, then generated with the reference to the new image names at every request.
In this case when the browser requests generated.jsp tomcat returns the page with 200 OK 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 old image name references.
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 304 status code.

看到这种行为,我检查了tomcat的工作目录,找到与 generated.jsp 对应的java和class文件,我找到了这些文件的时间戳大于webapp文件夹中包含的页面。

Seeing this behaviour, I checked tomcat's work directory to find the "java" and "class" files corresponding to generated.jsp and I found that those files has a timestamp older that the page contained in the webapp folder.

此时我尝试了另一种解决方法:使操作删除包含<的tomcat的工作子文件夹em> generated.jsp 相关文件。

即使此尝试失败,浏览器接收旧的 generated.jsp 页面和图像的304状态代码的行为也相同。

At this point I've tried another workaround: make the action delete tomcat's work subfolder containing generated.jsp related files.
Even this attempt failed, same behaviour with the browser receiving the old generated.jsp page and the 304 status code for images.

唯一可行的解​​决方法是删除webapp文件夹中的旧页面并创建具有不同名称的页面,就像我对图像所做的那样。

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.

我在文档中读到,如果资源被修改,Tomcat会在每次请求时检查,除非 web.xml 中的某些属性发生更改。 >
看到这种行为我相信tomcat使用一些缓存来进行检查并且没有每次都在文件系统上执行该操作,但我没有在文档中找到任何内容。

I read in the documentation that Tomcat does check at every request if a resource is modified, unless some properties in web.xml are changed.
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.

有人知道我是不是错了,也许可以帮助我理解这些是怎么回事机制在Tomcat中有效吗?

Does someone knows if I am wrong and maybe help me understand how those mechanism works in Tomcat?

推荐答案

Tomcat在服务器上缓存内容,因此修改运行时可用的资源会让你进入麻烦。你将在余下的职业生涯中尝试解决所有的问题并在此过程中疯狂。对不起,您需要重新考虑自己的方法。

Tomcat caches stuff on the server, so modifying the resources available at runtime is going to get you into trouble. You will spend the rest of your career trying to work-out all the kinks and go insane in the process. I'm sorry, you're going to need to re-think your approach.

为什么不在每个请求上生成代码等,为什么不提供请求100每次动态%,直接来自servlet?

Instead of generating code, etc. on each request, why not serve the request 100% dynamically each time, directly from the servlet?

这篇关于Tomcat 7返回“304 Not Modified”;在已修改/不存在的资源上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆