【问题标题】:How to set Expires HTTP header on a single JS file in Apache Tomcat?如何在 Apache Tomcat 中的单个 JS 文件上设置 Expires HTTP 标头?
【发布时间】:2015-01-07 22:39:28
【问题描述】:

我有一个js 文件,缓存时间在 5-10 分钟之间,具体取决于我是使用 eclipse 中的 tomcat(通过 GWT 插件)还是独立启动 tomcat。
这很奇怪,因为我使用GWT 作为我的框架,而且这个文件根本不应该被缓存(对于那些知道 GWT 的人来说,它是一个 nocache.js 文件)。 我在GWT Google group thread 上读到这是一个容器配置问题,在其他地方我需要在包含的 HTML 文件中定义它。
基本上,我现在很困惑,因为我不知道如何让这个文件不被缓存。 请注意这个js是GWT生成的,我不能修改。

感谢您的帮助, 伊泰

【问题讨论】:

    标签: tomcat caching gwt http-headers


    【解决方案1】:

    使用 javax.servlet.Filter

    以可移植的方式(跨不同的应用服务器)执行此操作的一种方法是使用过滤器。在您的 web.xml 中添加以下内容:

      <filter>
        <filter-name>headersFilter</filter-name>
        <filter-class>MyHeadersFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>headersFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    

    然后像这样实现你的 MyHeadersFilter:

    public class MyHeadersFilter implements Filter {
    
      @Override
      public void doFilter(final ServletRequest request,
             final ServletResponse response, final FilterChain chain)
             throws IOException, ServletException {
    
          final HttpServletRequest httpRequest = (HttpServletRequest) request;
          final String requestUri = httpRequest.getRequestURI();
    
          final HttpServletResponse httpResponse = (HttpServletResponse) response;
    
    
          if (requestUri.contains(".nocache.")) {
            httpResponse.addHeader("Cache-Control", "no-cache");
            ...
    
          } else if (...) {
            ...
          }
    
          chain.doFilter(request, response);
      }
    }
    

    可选:可配置的过滤器

    您还可以使用&lt;init-param&gt;s 使您的过滤器从您的 web.xml 配置:

      <filter>
        <filter-name>headersFilter</filter-name>
        <filter-class>MyHeadersFilter</filter-class>
        <init-param>
            <param-name>myParam</param-name>
            <param-value>myValue</param-value>
        </init-param>
      </filter>
    

    将以下内容添加到 MyHeadersFilter:

        private FilterConfig filterConfig;
    
        @Override
        public void init(final FilterConfig filterConfig) throws ServletException {
            this.filterConfig = filterConfig;
        }
    
        @Override
        public void destroy() {
            this.filterConfig = null;
        }
    

    这样就可以使用以下方法访问您的 init-param(s):

    filterConfig.getInitParameter("myParam")
    

    【讨论】:

    • 谢谢!在这样的过滤器中,如果我不使用任何外部资源或类属性,我应该在destroy()方法中做些什么吗?
    • 还有,这行得通吗? httpResponse.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");或者我应该写多个 httpResponse.addHeader("Cache-Control", "value1");httpResponse.addHeader("Cache-Control", "value2");等等……
    • @Ittai:当容器停止使用过滤器时,destroy 方法应该清理任何可以继续使用内存的实例字段,但无论出于何种原因都不会删除对过滤器的引用。来自 Javadoc:“此方法使过滤器有机会清理所持有的任何资源(例如,内存、文件句柄、线程)并确保任何持久状态与过滤器在内存中的当前状态同步。”
    • @Ittai: 你可以使用 addHeader("Cache-Control", "no-cache, no-store, must-revalidate")
    【解决方案2】:

    tomcat 7 中有一个可用的过滤器

    <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>
    </filter>
    <filter-mapping>
    <filter-name>ExpiresFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    

    您可以在此处找到更多详细信息

    https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/filters/ExpiresFilter.html

    【讨论】:

      猜你喜欢
      • 2014-05-16
      • 2011-01-12
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      相关资源
      最近更新 更多