如何在 Apache Tomcat 中的单个 JS 文件上设置 Expires HTTP 标头? [英] How to set Expires HTTP header on a single JS file in Apache Tomcat?

查看:34
本文介绍了如何在 Apache Tomcat 中的单个 JS 文件上设置 Expires HTTP 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a js file which is cached between 5-10 minutes, depending on whether I'm using tomcat from the eclipse (via GWT plugin) or starting tomcat as standalone.
This is strange as I'm using GWT as my framework and this file should not be cached at all (it's a nocache.js file to those of you who know GWT). I've read on a GWT Google group thread that it's a container configuration issue, and somewhere else that it's something I need to define in the containing HTML file.
Basically, I'm confused right now as I have no clue on how to get this file to not cache. Please note that this js is generated by GWT and I cannot modify it.

感谢您的帮助,伊泰

推荐答案

使用 javax.servlet.Filter

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

Using a javax.servlet.Filter

One way to do this in a portable way (across different app servers), is using Filters. In your web.xml add the following:

  <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:

Then implement your MyHeadersFilter like:

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);
  }
}

<小时>

可选:可配置的过滤器

您还可以使用 <init-param>s 使您的过滤器从您的 web.xml 配置:


Optional: Configurable Filters

You can also make your filter configurable from your web.xml, by using <init-param>s:

  <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:

Add the following to 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):

That makes it possible to access your init-param(s) using:

filterConfig.getInitParameter("myParam")

这篇关于如何在 Apache Tomcat 中的单个 JS 文件上设置 Expires HTTP 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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