过滤器 (Filter) 1、概述:过滤器概述过滤器就像一个保安。能够对请求和响应进行拦截。 2、编写过滤的步骤: 1)编写一个类。实现javax.servlet.Filter接口,这种类一般称之为过滤器类 public class FilterDemo1 implements Filter { public void init(FilterConfig filterConfig) throws ServletException { System.out.println("FilerDemo1初始化了"); } //对于每一次的用户訪问。都会调用该方法。 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("FilterDemo1拦截前"); chain.doFilter(request, response);//放行,让目标资源运行 System.out.println("FilterDemo1拦截后"); } public void destroy() { // TODO Auto-generated method stub } } 2)在web.xml中进行配置,要拦截哪些资源。 <filter> <filter-name>FilterDemo1</filter-name> <filter-class>cn.jxn.filter.FilterDemo1</filter-class> </filter> <filter-mapping> <filter-name>FilterDemo1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 3、过滤器的运行过程: 1)过滤器仅仅会被初始化一次。应用被载入时就完毕了初始化。 2)多个过滤器的拦截顺序是依照web.xml中filter-mapping元素的出现顺序进行拦截的。 4、过滤器简单案例: 1)界面输出中文及中文请求參数(POST方式有效)编码过滤器:SetCharacterEncodingFilter.java web.xml中的配置: <filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class>cn.jxn.filter.example.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> //界面输出中文及中文请求參数(POST方式有效)编码过滤器 public class SetCharacterEncodingFilter implements Filter { private FilterConfig filterConfig; public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String encodingValue = filterConfig.getInitParameter("encoding");//获取过滤器配置中的參数 if(encodingValue==null)//假设没有配置该參数 encodingValue="UTF-8";//给定默认值UTF-8 request.setCharacterEncoding(encodingValue);//仅仅对POST请求方式实用 response.setContentType("text/html;charset="+encodingValue); chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } } 2)控制动态资源(Servlet JSP)不要缓存的过滤器 web.xml中的配置 <filter> <filter-name>NoCacheFilter</filter-name> <filter-class>cn.jxn.filter.example.NoCacheFilter</filter-class> </filter> <filter-mapping> <filter-name>NoCacheFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>NoCacheFilter</filter-name> <url-pattern>/servlet/*</url-pattern> </filter-mapping> // 控制动态资源(Servlet JSP)不要缓存的过滤器 public class NoCacheFilter implements Filter { public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)resp; response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { } } 3)控制html、css、js等静态资源的缓存时间的过滤器 web.xml中的配置 <filter> <filter-name>NeedCacheFilter</filter-name> <filter-class>cn.jxn.filter.example.NeedCacheFilter</filter-class> <init-param> <param-name>html</param-name> <param-value>1</param-value><!-- 单位是小时 --> </init-param> <init-param> <param-name>css</param-name> <param-value>2</param-value><!-- 单位是小时 --> </init-param> <init-param> <param-name>js</param-name> <param-value>3</param-value><!-- 单位是小时 --> </init-param> </filter> <filter-mapping> <filter-name>NeedCacheFilter</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> <filter-mapping> <filter-name>NeedCacheFilter</filter-name> <url-pattern>*.css</url-pattern> </filter-mapping> <filter-mapping> <filter-name>NeedCacheFilter</filter-name> <url-pattern>*.js</url-pattern> </filter-mapping> public class NeedCacheFilter implements Filter { private FilterConfig filterConfig; public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { System.out.println("控制缓存时间的过滤器过滤了"); HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)resp; //获取html、css、js各自的缓存时间。假设没有。给个默认值是1小时 int time = 1; String uri = request.getRequestURI();// /day19/1.html String extendName = uri.substring(uri.lastIndexOf(".")+1); if("html".equals(extendName)){ //訪问的html资源 String value = filterConfig.getInitParameter("html"); if(value!=null){ time = Integer.parseInt(value); } } if("css".equals(extendName)){ //訪问的css资源 String value = filterConfig.getInitParameter("css"); if(value!=null){ time = Integer.parseInt(value); } } if("js".equals(extendName)){ //訪问的js资源 String value = filterConfig.getInitParameter("js"); if(value!=null){ time = Integer.parseInt(value); } } response.setDateHeader("Expires", System.currentTimeMillis()+time*60*60*1000); chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } }