使用java开发,一般我们可以通过filter来控制某些内容添加不同的响应头。
以下的示例示例了如何为不同类型的文件添加不同的Cache-Control。
public class ResponseHeaderFilter implements Filter {
FilterConfig fc;
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
// set the provided HTTP response parameters
for (Enumeration e = fc.getInitParameterNames(); e.hasMoreElements();) {
String headerName = (String) e.nextElement();
response.addHeader(headerName, fc.getInitParameter(headerName));
}
// pass the request/response on
chain.doFilter(req, response);
}
public void init(FilterConfig filterConfig) {
this.fc = filterConfig;
}
public void destroy() {
this.fc = null;
}
}
web.xml中的配置
NoCache
apis.server.common.util.ResponseHeaderFilter
Cache-Control
no-cache, must-revalidate
CacheForWeek
apis.server.common.util.ResponseHeaderFilter
Cache-Control
max-age=604800, public
NoCache
*.do
CacheForWeek
/images/*
CacheForWeek
/img/*
CacheForWeek
/icons/*
CacheForWeek
/ext/*
CacheForWeek
*.js
CacheForWeek
*.css
阅读(1579) | 评论(0) | 转发(0) |