如何在 Apache Tomcat 中的单个 JS 文件上设置 Expires HTTP 标头?

发布于 2024-09-15 10:26:50 字数 564 浏览 5 评论 0原文

我有一个 js 文件,该文件缓存 5-10 分钟,具体取决于我是从 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.

Thanks for any help,
Ittai
alt text
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一世旳自豪 2024-09-22 10:26:50

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

:可配置过滤器

您还可以使用 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:

filterConfig.getInitParameter("myParam")

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>

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

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>

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

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

filterConfig.getInitParameter("myParam")
少年亿悲伤 2024-09-22 10:26:50

tomcat 7 中有一个 Filter 可用,

<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

There is a Filter available in 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>

You can find more details here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文