【发布时间】:2014-08-12 22:52:27
【问题描述】:
大家,
我的 webapp 处理浏览器缓存的方式似乎有问题。我试着用谷歌搜索了一下,但没有找到任何相关的东西。
为了处理缓存,我将以下代码放入我的 web.xml 中:
<filter>
<filter-name>Cache filter</filter-name>
<filter-class>My_personnal_class</filter-class>
</filter>
<filter-mapping>
<filter-name>Cache filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
并使用这个类:
public class my_personnal_class implements Filter {
public void destroy() {}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)throws IOException,
ServletException {
long time = 5 * 1000; // 5 secondes
if (((HttpServletRequest)req).getRequestURI().indexOf("/a path/") != -1) {
time= 20 * 60 * 1000; // 20 minutes
}
if (((HttpServletRequest)req).getRequestURI().indexOf("/yet another path/") != -1) {
time= 20 * 60 * 1000; // 20 minutes
}
temps = temps + System.currentTimeMillis();
((HttpServletResponse)resp).setDateHeader("Expires", time);
// Now let the request go through other filters and the servlet
chain.doFilter(req, resp);
}
public void init(FilterConfig filterConfig)
throws ServletException {}
这样,我确实将缓存过期日期设置为 20 分钟。 (我可以在值得信赖的老萤火虫的网络标签中看到它。)
问题是缓存似乎没有被使用。当我重新加载页面时,服务器不会在缓存中获取数据,而是向我发送“304 - 未修改”响应。 另外,如果我修改了一个资源(比如说一个 HTML 页面),浏览器会去服务器上获取它(完全!200 响应,我会显示新版本),即使到期日期尚未满足。
我知道 304 响应对于带宽来说没什么大不了的,但仍然......我想知道这种行为是否正常。我做错了什么(或理解错了)?
任何关于这件事的信息都会有很大的帮助。
谢谢。
【问题讨论】:
标签: java caching web-applications