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

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

问题描述

我有一个 js 文件,这个文件会在5-10分钟之间缓存,这取决于我是使用eclipse中的tomcat(通过GWT插件)还是将tomcat作为启动文件独立。

这很奇怪,因为我使用 GWT 作为我的框架,并且该文件根本不应该被缓存(这是一个nocache.js文件对那些了解GWT的人)。
我阅读过 GWT Google小组讨论,这是一个容器配置问题,并在别的地方,这是我需要在包含HTML文件中定义的东西。

基本上,我现在很困惑,因为我不知道如何得到这个文件不缓存。
请注意这个js是由 GWT 产生的,我无法修改它。



帮助,
Ittai


解决方案

使用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 $ b $ public void doFilter(final ServletRequest request,$ b $ final ServletResponse response,final FilterChain chain)
抛出IOException,ServletException {

final HttpServletRequest httpRequest =(HttpServletRequest)请求;
final String requestUri = httpRequest.getRequestURI();

final HttpServletResponse httpResponse =(HttpServletResponse)响应;


if(requestUri.contains(.nocache。)){
httpResponse.addHeader(Cache-Control,no-cache);
...

} else if(...){
...
}

chain.doFilter(request,响应);


$ / code $ / pre

$ hr

可选:可配置的过滤器



您还可以使用< init-param> s:

 < filter> 
< filter-name> headersFilter< / filter-name>
< filter-class> MyHeadersFilter< / filter-class>
< init-param>
< param-name> myParam< / param-name>
<参数值> myValue< /参数值>
< / 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;



$ b $ p
$ b

这样可以使用以下方法访问init-param: / p>

  filterConfig.getInitParameter(myParam)


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

解决方案

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")

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

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