[英]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. 我有一个js
文件,它会在5-10分钟之间缓存,具体取决于我是使用eclipse中的tomcat(通过GWT插件)还是单独启动tomcat。
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). 这很奇怪,因为我使用GWT
作为框架,并且根本不应该缓存该文件(对那些了解GWT的人来说,这是一个nocache.js文件)。 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. 我在GWT Google组线程上读到这是一个容器配置问题,而在其他地方,我需要在包含HTML文件中定义它。
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. 请注意,此js是由GWT
生成的,我无法对其进行修改。
Thanks for any help, Ittai 感谢您的帮助,伊泰
One way to do this in a portable way (across different app servers), is using Filters. 一种可移植的方式(在不同的应用程序服务器之间)执行此操作的一种方法是使用过滤器。 In your web.xml add the following: 在您的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>
Then implement your MyHeadersFilter like: 然后像这样实现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);
}
}
You can also make your filter configurable from your web.xml, by using <init-param>
s: 您还可以使用<init-param>
来使过滤器可从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>
Add the following to MyHeadersFilter: 将以下内容添加到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: 这样就可以使用以下命令访问您的init-param。
filterConfig.getInitParameter("myParam")
There is a Filter available in tomcat 7 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 https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/filters/ExpiresFilter.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.