Java JBOSS服务器上的web应用缓存-未使用缓存
各位, 我的webapp处理浏览器缓存的方式似乎有问题。我试着用谷歌搜索一下,但没有找到任何相关的东西 为了处理缓存,我在web.xml中添加了以下代码:Java JBOSS服务器上的web应用缓存-未使用缓存,java,caching,web-applications,Java,Caching,Web Applications,各位, 我的webapp处理浏览器缓存的方式似乎有问题。我试着用谷歌搜索一下,但没有找到任何相关的东西 为了处理缓存,我在web.xml中添加了以下代码: <filter> <filter-name>Cache filter</filter-name> <filter-class>My_personnal_class</filter-class> </filter> <filter-mapping&
<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>
这样,缓存的过期日期设置为20分钟。我可以在我信任的老firebug的网络选项卡上看到它
问题是缓存似乎没有被使用。当我重新加载页面时,服务器不会在缓存中获取数据,而是向我发送304-非修改响应。
另外,如果我修改了一个资源,比如说一个HTML页面,浏览器将在服务器上完全获取它!200响应,即使到期日期尚未满足,我也会显示新版本
我知道304个回答对乐队来说不是什么大不了的事,但是。。我想知道这种行为是否正常。我做错了什么或理解错了什么
关于那件事的任何信息都会大有帮助
谢谢。好的,我终于明白了,上面的代码工作正常 重新加载页面时,浏览器将尝试与服务器一起检查每个资源。然后,他将使用etag将缓存内容与服务器上的内容进行比较,并发出304个响应 因此,永远不要通过重新加载页面f5来测试http缓存设置。只需跟随链接,或使用书签即可 容易犯错误的人
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 {}