Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

caching - How to set Expires HTTP header on a single JS file in Apache Tomcat?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...