您的位置:首页 > 编程语言 > Java开发

java 过滤器详细知识

2015-07-30 16:11 525 查看
过滤器是一个服务端的组件,它可以截取用户端的请求和响应信息,并对这些信息过滤

过滤器工作原理

用户请求经过过滤器判断后 过滤器将用户请求发送至web资源 web资源将资源响应发送至过滤器 过滤器将web资源的响应发送给用户

过滤器的生命周期

实例化 web.xml配置 在web容器启动后实例化一次

初始化 init方法

过滤   doFilter

销毁   destroy()

过滤器可以改变用户请求的web资源,改变用户请求的路径

过滤器不可以直接返回用户数据

多个过滤器用户请求会先执行放行前的过滤器1的方法 在执行chin.doFilter方法到达 过滤器2 执行过滤器2 的放行前的方法 接着执行chain.doFilter到达

servlet service方法 然后返回过滤器2 执行过滤器2 放行后的方法 在执行过滤器1 放行后的方法 最后反馈给用户请求
下面为Filter链的实例

public class FirstFilter implements Filter{

    public void destroy() {

        // TODO Auto-generated method stub

        System.out.println("firstfilter destroy");

    }

    public void doFilter(ServletRequest request, ServletResponse response,

            FilterChain chain) throws IOException, ServletException {

        // TODO Auto-generated method stub

        System.out.println("firstfilter  start");

        chain.doFilter(request, response);

        System.out.println("firstfilter end");

    }

    public void init(FilterConfig filterConfig) throws ServletException {

        // TODO Auto-generated method stub

        System.out.println("firstfilter init");

    }

}

public class SecondFilter implements Filter{

    public void destroy() {

        // TODO Auto-generated method stub

        System.out.println("second filter destroy");

    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1,

            FilterChain arg2) throws IOException, ServletException {

        // TODO Auto-generated method stub

        System.out.println("sencond filter start");

        arg2.doFilter(arg0, arg1);

        System.out.println("second filter end");

    }

    public void init(FilterConfig arg0) throws ServletException {

        // TODO Auto-generated method stub

        System.out.println("second filter init");

    }

}

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <display-name></display-name>    

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

  <filter>

      <filter-name>first</filter-name>

      <filter-class>com.gac.test.FirstFilter</filter-class>

  </filter>

  <filter-mapping>

      <filter-name>first</filter-name>

      <url-pattern>/index.jsp</url-pattern>

  </filter-mapping>

 

   <filter>

      <filter-name>second</filter-name>

      <filter-class>com.gac.test.SecondFilter</filter-class>

  </filter>

  <filter-mapping>

      <filter-name>second</filter-name>

      <url-pattern>/index.jsp</url-pattern>

  </filter-mapping>

</web-app>

打印结果为

second filter init

firstfilter init

2015-7-30 14:41:41 org.apache.coyote.http11.Http11Protocol start

信息: Starting Coyote HTTP/1.1 on http-8080

2015-7-30 14:41:41 org.apache.jk.common.ChannelSocket init

信息: JK: ajp13 listening on /0.0.0.0:8009

2015-7-30 14:41:41 org.apache.jk.server.JkMain start

信息: Jk running ID=0 time=1/10  config=null

2015-7-30 14:41:41 org.apache.catalina.startup.Catalina start

信息: Server startup in 361 ms

firstfilter  start

sencond filter start

//这里处理jsp页面

second filter end

firstfilter end

servlet2.5过滤器分为

REQUEST 用户直接访问页面时,web容器将会调用过滤器

FORWARD 目标用户通过RequestDispatcher的forward访问时该过滤器被调用 在jsp页面中引入<jsp:forward page="main.jsp"></jsp:forward>标签 就会调用过滤器forward配置的过滤器

INCLUDE 目标资源通过RequestDispatcher的include方法调用时,该过滤器被调用 在jsp页面中引入<jsp:include page=""></jsp:include>

ERROR   目标资源通过声明式异常处理机制调用时,过滤器将被调用。

Servelt3.0 ASYNC 支持异步处理 @WebFilter 用于将一个类声明为过滤器,该注解将会在部署时被容器处理,容器根据具体的属性配置将相应

的类部署为过滤器。 @WebFilter常用属性:??
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: