Tomcat:缓存控制 [英] Tomcat: Cache-Control

查看:421
本文介绍了Tomcat:缓存控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jetty有 CacheControl参数(可以指定webdefault.xml)确定客户端的缓存行为(通过影响发送给客户端的标头)。

Jetty has a CacheControl parameter (can be specified webdefault.xml) that determines the caching behavior of clients (by affecting headers sent to clients).

Tomcat是否有类似的选项?
简而言之,我想关闭tomcat服务器和/或特定网络应用程序提供的所有页面的缓存?

Does Tomcat has a similar option? In short, I want to turn off caching of all pages delivered by a tomcat server and/or by a specific webapp?

更新

请注意,我不是指服务器端缓存。我希望服务器告诉所有客户端(浏览器)不要使用自己的缓存并始终从服务器获取内容。我想一次为所有资源做这件事,包括静态资源(.css,.js等)。

Please note that I am not referring to server-side caching. I want the server to tell all clients (browsers) not to use their own cache and to always fetch the content from the server. I want to do it for all resources, including static resources (.css, .js, etc.) at once.

推荐答案

与上面的帖子类似,除了该代码存在一些问题。这将禁用所有浏览器缓存:

Similar to the post above, except there are some issues with that code. This will disable all browser caching:

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class CacheControlFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletResponse resp = (HttpServletResponse) response;
        resp.setHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT");
        resp.setDateHeader("Last-Modified", new Date().getTime());
        resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
        resp.setHeader("Pragma", "no-cache");

        chain.doFilter(request, response);
    }

}

然后在web.xml中映射如 Stu Thompson的答案中所述。

and then map in web.xml as described in Stu Thompson's answer.

这篇关于Tomcat:缓存控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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