javagae与响应缓存
下午好, 我需要我的Servlet的结果总是由浏览器缓存。尝试放置相同的头,比如http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js,但结果仍然不是来自浏览器缓存(使用FireBug测试)
我的代码:
response.setContentType("text/javascript");
response.setHeader("Last-Modified", "Mon, 15 Feb 2010 23:30:12 GMT");
response.setHeader("Date", "Tue, 28 Sep 2010 19:45:24 GMT");
response.setHeader("Expires", "Wed, 28 Sep 2021 19:45:24 GMT");
response.setHeader("Vary", "Accept-Encoding");
response.setHeader("X-Content-Type-Options", "nosniff");
response.setHeader("Cache-Control", "public, max-age=31536000");
response.setHeader("Age", "36");
我的标题(firebug):
HTTP/1.1 200 OK
Content-Type: text/javascript; charset=UTF-8
Last-Modified: Mon, 15 Feb 2010 23:30:12 GMT
Date: Sun, 10 Oct 2010 14:40:49 GMT
Expires: Mon, 10 Oct 2011 14:40:49 GMT
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Server: sffe
Content-Encoding: gzip
Cache-Control: public, max-age=31536000
Content-Length: 46401
Age: 2
Googleapis标题:
HTTP/1.1 200 OK
Content-Type: text/javascript; charset=utf-8
Last-Modified: Mon, 15 Feb 2010 23:30:12 GMT
Expires: Wed, 28 Sep 2021 19:45:24 GMT
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Cache-Control: public, max-age=31536000
Age: 36
Content-Encoding: gzip
Date: Sun, 10 Oct 2010 14:32:06 GMT
Server: Google Frontend
Content-Length: 36838
所有的头都是相同的,但顺序不同(即使代码不应该这样,显然GAE会颠倒顺序)。 到底有什么问题? 谢谢
# 1 楼答案
改用addHeader。setHeader()覆盖之前设置的值
# 2 楼答案
点餐并不重要。你有问题吗?理论上,如果GAE使用的servletcontainer在提交到响应之前使用
HashMap
存储头,而您的本地servletcontainer使用LinkedHashMap
或List<Pair>
,则可能会发生这种情况至于响应缓存,您至少需要一个遥远的
Expires
头以及一个有效的ETag
或Last-Modified
头。不需要Cache-Control
和Age