java Tomcat:Cache-Control

vkc1a9a2  于 2023-06-28  发布在  Java
关注(0)|答案(7)|浏览(118)

Jetty有一个CacheControl parameter(可以指定为webdefault.xml),它决定了客户端的缓存行为(通过影响发送到客户端的头)。
Tomcat有类似的选项吗?简而言之,我想关闭对tomcat服务器和/或特定webapp提供的所有页面的缓存?

更新

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

1rhkuytd

1rhkuytd1#

自Tomcat 7以来,提供了一个容器过期过滤器,可能会有所帮助。参见:

  • Tomcat 10:https://tomcat.apache.org/tomcat-10.0-doc/config/filter.html#Expires_Filter
  • Tomcat 9:https://tomcat.apache.org/tomcat-9.0-doc/config/filter.html#Expires_Filter
  • Tomcat 8:https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html#Expires_Filter
  • Tomcat 7:https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter
  • Tomcat 6(非官方backport):https://github.com/bnegrao/ExpiresFilter

ExpiresFilter是Apache mod_expires的Java Servlet API端口。此过滤器控制服务器响应中Expires HTTP标头和Cache-Control HTTP标头的max-age指令的设置。过期日期可以设置为与上次修改源文件的时间或客户端访问的时间相关。

<filter>
    <filter-name>ExpiresFilter</filter-name>
    <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
    <init-param>
        <param-name>ExpiresByType image</param-name>
        <param-value>access plus 10 days</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType text/css</param-name>
        <param-value>access plus 10 hours</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType application/javascript</param-name>
        <param-value>access plus 10 minutes</param-value>
    </init-param>
    <!-- Let everything else expire immediately -->
    <init-param>
        <param-name>ExpiresDefault</param-name>
        <param-value>access plus 0 seconds</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>ExpiresFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
qcuzuvrc

qcuzuvrc2#

类似于上面的文章,除了代码有一些问题。这将禁用所有浏览器缓存:

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中进行Map,如Stu Thompson's answer中所述。

lb3vh1jj

lb3vh1jj3#

我不相信有配置可以做到这一点。但是,编写一个过滤器来设置每个webapp的Cache-Control头不应该花费太多精力。例如:

public class test implements Filter {

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

            chain.doFilter(request, response);
            ((StatusResponse)response).setHeader("Cache-Control",
                    "max-age=0, private, must-revalidate");
        }

        public void destroy() {}

        public void init(FilterConfig arg0) throws ServletException {}
}

然后将此代码段放入webapp的web.xml文件中。

<filter>
    <filter-name>SetCacheControl</filter-name>
    <filter-class>ch.dietpizza.cacheControlFilter</filter-class>
</filter>                       
<filter-mapping>
    <filter-name>SetCacheControl</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
l7mqbcuq

l7mqbcuq4#

实际上,Tomcat配置中有几个元素直接影响这一点。例如,请参阅http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html上的文档。
Atlassian建议使用以下两个语句来启用浏览器端缓存,以便Microsoft Internet Explorer能够正确下载和查看附加文档:

<Valve className="org.apache.catalina.authenticator.FormAuthenticator" securePagesWithPragma="false" />
<Valve className="org.apache.catalina.authenticator.NonLoginAuthenticator" securePagesWithPragma="false" />
ehxuflar

ehxuflar5#

这可能是你正在寻找的:
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Context%20Parameters

cachingAllowed : If the value of this flag is true, the cache for static

 resources will be used. If not specified, the default value of the flag is true.

在更改此标志后,还要删除/work/ Catalina /localhost中的应用程序缓存文件夹。

6yoyoihd

6yoyoihd6#

我知道的唯一参数是<Valve>元素上的disableProxyCaching。参见here

laik7k3q

laik7k3q7#

我研究的几乎所有答案都来自服务器端,实际上客户端也需要工作。在jsp/html中需要在header下面添加。

<META http-equiv="Expires" CONTENT="0">  
<META http-equiv="Cache-Control" CONTENT="no-cache">  
<META http-equiv="Pragma" CONTENT="no-cache">

相关问题