Filter

来源:互联网 发布:停车源码 编辑:程序博客网 时间:2024/06/05 07:49
  过滤器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.javaweb.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-8request.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.htmlString 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;}}

0 0
原创粉丝点击