首页 > 技术文章 > 过滤器filter

ly-china 2016-04-22 20:10 原文

自定义filter 

  需要实现filter接口,重写doFilter方法  tomcat服务器启动的时候先实例化过滤器 -----》 再初始化过滤器

请求执行顺序 请求---》过滤器doFilter方法---》doFilter方法 chain.doFilter放行---》servlet的doGet()或者doPost()方法业务处理---》过滤器的doFilter方法

多个过滤器的配置顺序就是请求经过的顺序

FilterConfig 获取该过滤器一些配置信息

FilterChain 过滤器参数  所有的过滤器连成一个链

 

---------------------------web.xml配置-----------------------

<filter>

  <filter-name>名称</filter-name>                          与下面的名称要一致

  <filter-class>过滤器的全名(包括包名)</filter-class>

</filter>

<filter-mapping>

  <filter-name>名称</filter-name>

  <url-pattern>什么样的url请求地址要拦截</url-pattern>

//例如:1,/* 代表所有的资源都得过滤    2,可以写servlet的名称来对该servlet进行过滤  3,可以根据类型来过滤配置如下

<dispatcher>REQUEST<dispatcher>默认

<dispatcher>FORWARD<dispatcher>拦截转发

<dispatcher>INCLUDE<dispatcher>拦截包含url的页面(requestDispatcher.include("url"))        声明式异常就是在web.xml配置

<dispatcher>ERROR<dispatcher>拦截声明式异常信息                    <error>

                                               <error-code>404<error-code>发生错误信息

                                            也可以<exception></exception>声明类型

                                               <location>发生错误 转向那个页面<location> 

                                             </error>

 

</filter-mapping>

推荐阅读