我希望缓存网页中的 .png 文件。我在 web.xml 中添加了以下条目
<filter>
<filter-name>ContentFilter</filter-name>
<filter-class>filters.ContentFilter</filter-class>
<init-param>
<description>Add an Expires Header</description>
<param-name>expiryDate</param-name>
<param-value>Fri, 30 Apr 2021 20:00:00 GMT</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ContentFilter</filter-name>
<url-pattern>*.png</url-pattern>
</filter-mapping>
在 init() 中按以下方式设置 expiryDate 字段值
String expiryDateStr = filterConfig.getInitParameter("expiryDate");
SimpleDateFormat format = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss Z");
try {
Date d = format.parse(expiryDateStr);
expiryDate = d.getTime();
} catch (ParseException e) {
logger.error(e.getMessage(), e);
}
doFilter() 是:
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain filChain) throws IOException, ServletException {
logger.debug("doFilter()");
logger.info(((HttpServletRequest)req).getRequestURL().toString());
filChain.doFilter(req, res);
if (res instanceof HttpServletResponse) {
HttpServletResponse response = (HttpServletResponse) res;
logger.info(((HttpServletRequest)req).getRequestURL().toString());
response.setDateHeader("Expires", expiryDate);
}
}
我的问题是,每当我在浏览器中刷新网页时,客户端都会不断请求 .png 文件。猜猜我的过滤器不工作。这个配置正确吗?