91.1过滤器编程接口 进行过滤器编程用到 javax. servlet. jar中的一组接 口和类,表9-1只列出了与过滤器设计有关的三个重要 接口,而与 Servlet编程有关的接口、类请参考第6章。 表9- I Servlet编程接口 功能 类和接口 filter实现 javax. servlet Filter Filter配置 javax. servlet Filter Config Filter链 javax. servlet Filter Chain
9.1.1 过滤器编程接口 进行过滤器编程用到javax.servlet.jar中的一组接 口和类,表9-1只列出了与过滤器设计有关的三个重要 接口,而与Servlet编程有关的接口、类请参考第6章。 表9-1 Servlet编程接口 功能 类和接口 Filter实现 javax.servlet.Filter Filter配置 javax.servlet.FilterConfig Filter链 javax.servlet.FilterChain
接口 Filter的主要方法: (1)init(方法 方法原型: public void init(Filter Config filterConfig) throws ServletException仆} 该方法用于初始化过滤器,并获取 web .xm|文件中 配置的过滤器初始化参数,默认情况下,服务器启动 时就会加载过滤器,init方法就会执行。 该方法有一个 Filter Config类型的参数,利用它可以 获取在 web. xm|中设置的过滤器的初始化参数值。获取 初始参数值的方法为: public String getInitParameter(String para Name 过滤器的信息配置将在91.3节中介绍
接口Filter的主要方法: (1)init()方法 方法原型: public void init(FilterConfig filterConfig) throws ServletException{} 该方法用于初始化过滤器,并获取web.xml文件中 配置的过滤器初始化参数,默认情况下,服务器启动 时就会加载过滤器,init方法就会执行。 该方法有一个FilterConfig类型的参数,利用它可以 获取在web.xml中设置的过滤器的初始化参数值。获取 初始参数值的方法为: public String getInitParameter(String paraName) 过滤器的信息配置将在9.1.3节中介绍
接口 Filter的主要方法: (2) dofilter()方法 方法原型: public void doFilter( Servletrequest request, Servletresponse response Filter Chain filter chain) throws lOEXception Servlet Exception 当请求地址和过滤地址匹配时将进行过滤操作,该方法 被执行。 第1个参数为 Servletrequest对象,此对象给过滤器提供了 对请求信息(包括表单数据、Cookie和HTTP请求头)的完全 访问。 第2个参数为 IServletResponse,用于响应请求 第3个参数为 Filter Chain对象,使用该参数对象调用 Servlet、 JSP页面或者过滤器链中的下一个过滤器。 调用方法为: public void doFilter(ServletRequest request, ServletResponse response)
接口Filter的主要方法: (2)doFilter()方法 方法原型: public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,ServletException{} 当请求地址和过滤地址匹配时将进行过滤操作,该方法 被执行。 第1个参数为ServletRequest对象,此对象给过滤器提供了 对请求信息(包括表单数据、Cookie和HTTP请求头)的完全 访问。 第2个参数为ServletResponse,用于响应请求。 第3个参数为FilterChain对象,使用该参数对象调用Servlet、 JSP页面或者过滤器链中的下一个过滤器。 调用方法为: public void doFilter(ServletRequest request, ServletResponse response)
接口 Filter的主要方法: (3) destroy()方法 方法原型: public void destroy Servlet容器在销毁过滤器实例前调用该方法,这 个方法中可以释放 Servlet过滤器占用的资源 性质等同于 Servlet的 destory()方法。 这些方法构成了过滤器对象的生命周期:创建、 执行过滤方法、销毁
接口Filter的主要方法: (3)destroy()方法 方法原型:public void destroy() Servlet容器在销毁过滤器实例前调用该方法,这 个方法中可以释放Servlet过滤器占用的资源。 性质等同于Servlet的destory()方法。 这些方法构成了过滤器对象的生命周期:创建、 执行过滤方法、销毁
912设计过滤器 过滤器的设计需要实现 Filter接口,并要根据处 理的功能需要,实现 filter接口中的3个方法: 1 public void init(Filter Config filter Config) throws ServletException 2 public void do Filter Servlet request request ServletResponse response Filter Chain filterChain)throws lOException, ServletException 3 public void destroyo
9.1.2 设计过滤器 过滤器的设计需要实现Filter接口,并要根据处 理的功能需要,实现Filter接口中的3个方法: 1、public void init(FilterConfig filterConfig) throws ServletException 2、public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,ServletException 3、public void destroy()