日期:2014-05-16 浏览次数:20327 次
1、在web.xml配置
<filter> <filter-name>CacheForWeek</filter-name> <filter-class> com.sencloud.dh.core.web.filter.ResponseHeaderFilter </filter-class> <init-param> <param-name>Cache-Control</param-name> <param-value>max-age=604800, public</param-value> </init-param> </filter> <filter-mapping> <filter-name>CacheForWeek</filter-name> <url-pattern>/images/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CacheForWeek</filter-name> <url-pattern>/ext3/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CacheForWeek</filter-name> <url-pattern>/js/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CacheForWeek</filter-name> <url-pattern>/css/*</url-pattern> </filter-mapping>2、java代码
/** * * @author xutianlong * @version [版本号, Sep 11, 2012] * @see [相关类/方法] * @since [产品/模块版本] */ public class ResponseHeaderFilter implements Filter { private FilterConfig fc; public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; for (Enumeration e = this.fc.getInitParameterNames(); e.hasMoreElements();) { String headerName = (String) e.nextElement(); response.addHeader(headerName, this.fc.getInitParameter(headerName)); } chain.doFilter(req, response); } public void init(FilterConfig filterConfig) { this.fc = filterConfig; } public void destroy() { this.fc = null; } }