首页 > Java开发 > java web servlet 过滤器filter与过滤器链filterchain的执行过程分析与验证

java web servlet 过滤器filter与过滤器链filterchain的执行过程分析与验证

涉及的类:filterweb.tony.filter.Filter1,filterweb.tony.filter.Filter2。对应的filter名分别为:filter1,filter2。类代码见文章最后

 

 

1、在web服务器启动时会加载web.xml中配置的所有filter。验证:

开启tomcat服务器后提示

信息: Deploying web application directory F:\MTony\Workspaces\MyEclipse Professional\.metadata\.me_tcat7\webapps\filterwebFilter1.initFilter2.init

 

2、filter链的形成:

当请求一个资源时,服务器会查询web.xml中所有对此资源路径进行过滤的filter,并根据在web.xml中的先后顺序形成一个filter链(filterchain)

(1)根据在web.xml中的先后顺序形成一个filter链。验证

 

[html][/html] view plaincopy

  1. <filter>
  2.     <filter-name>filter2</filter-name>
  3.     <filter-class>filterweb.tony.filter.Filter2</filter-class>
  4. </filter>
  5. <filter-mapping>
  6.     <filter-name>filter2</filter-name>
  7.     <url-pattern>/*</url-pattern>
  8. </filter-mapping>
  9. <filter>
  10.     <filter-name>filter1</filter-name>
  11.     <filter-class>filterweb.tony.filter.Filter1</filter-class>
  12. </filter>
  13. <filter-mapping>
  14.     <filter-name>filter1</filter-name>
  15.     <url-pattern>/*</url-pattern>
  16. </filter-mapping>

浏览器中请求http://127.0.0.1:9090//filterweb/index.jsp,服务器发现web.xml中filter1和filter2都会对此路径过滤,按照顺序形成filterchain:filter2----->filter1。控制台输出信息:

 

 

[plain][/plain] view plaincopy

  1. Filter2.doFilter.前
  2. Filter1.doFilter.前
  3. Filter1.doFilter.后
  4. Filter2.doFilter.后

将web.xml中filter的顺序反过来

 

 

[html][/html] view plaincopy

  1. <filter>
  2.     <filter-name>filter1</filter-name>
  3.     <filter-class>filterweb.tony.filter.Filter1</filter-class>
  4. </filter>
  5. <filter-mapping>
  6.     <filter-name>filter1</filter-name>
  7.     <url-pattern>/*</url-pattern>
  8. </filter-mapping>
  9. <filter>
  10.     <filter-name>filter2</filter-name>
  11.     <filter-class>filterweb.tony.filter.Filter2</filter-class>
  12. </filter>
  13. <filter-mapping>
  14.     <filter-name>filter2</filter-name>
  15.     <url-pattern>/*</url-pattern>
  16. </filter-mapping>

 

 

浏览器中请求http://127.0.0.1:9090//filterweb/index.jsp,服务器发现web.xml中filter1和filter2都会对此路径过滤,按照顺序形成filterchain:filter2----->filter1。控制台输出信息:

 

[plain][/plain] view plaincopy

  1. Filter1.doFilter.前
  2. Filter2.doFilter.前
  3. Filter2.doFilter.后
  4. Filter1.doFilter.后

(2)根据请求的资源路径选择filter。例如现在将filter1的过滤路径设置为/common/*,即common下的所有资源。

 

[html][/html] view plaincopy

  1. <filter>
  2.     <filter-name>filter1</filter-name>
  3.     <filter-class>filterweb.tony.filter.Filter1</filter-class>
  4. </filter>
  5. <filter-mapping>
  6.     <filter-name>filter1</filter-name>
  7.     <url-pattern>/common/*</url-pattern>
  8. </filter-mapping>

并在common目录下新建一个common_index.jsp。

 

 

浏览器中请求http://127.0.0.1:9090//filterweb/index.jsp,服务器发现web.xml中只有filter2会对此路径过滤。控制台输出信息

 

[plain][/plain] view plaincopy

  1. Filter2.doFilter.前
  2. Filter2.doFilter.后

浏览器中请求http://127.0.0.1:9090//filterweb/common/common_index.jsp,服务器发现web.xml中filter1和filter2会对此路径过滤。控制台输出信息

 

 

[plain][/plain] view plaincopy

  1. Filter2.doFilter.前
  2. Filter1.doFilter.前
  3. Filter1.doFilter.后
  4. Filter2.doFilter.后

 

 

 

(3)执行顺序:【filter1的dofilter()前------>filter1的dofilter()】======>【filter2的dofilter()前------>filter2的dofilter()------>filter2的dofilter()后】======>【filter1的dofilter()后】

 

可以从(1)的结果中看出来

 

 

 

filterweb.tony.filter.Filter1.java

 

[java][/java] view plaincopy

  1. package filterweb.tony.filter;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. public class Filter1 implements Filter {
  10.     @Override
  11.     public void init(FilterConfig arg0) throws ServletException {
  12.         System.out.println("Filter1.init");
  13.     }
  14.     @Override
  15.     public void destroy() {
  16.         System.out.println("Filter1.destroy");
  17.     }
  18.     @Override
  19.     public void doFilter(ServletRequest req, ServletResponse resp,
  20.             FilterChain chain) throws IOException, ServletException {
  21.         System.out.println("Filter1.doFilter.前");
  22.         chain.doFilter(req, resp);
  23.         System.out.println("Filter1.doFilter.后");
  24.     }
  25. }

 

 

filterweb.tony.filter.Filter2.java

 

[java][/java] view plaincopy

  1. package filterweb.tony.filter;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. public class Filter2 implements Filter {
  10.     @Override
  11.     public void init(FilterConfig arg0) throws ServletException {
  12.         System.out.println("Filter2.init");
  13.     }
  14.     @Override
  15.     public void destroy() {
  16.         System.out.println("Filter2.destroy");
  17.     }
  18.     @Override
  19.     public void doFilter(ServletRequest req, ServletResponse resp,
  20.             FilterChain chain) throws IOException, ServletException {
  21.         System.out.println("Filter2.doFilter.前");
  22.         chain.doFilter(req, resp);
  23.         System.out.println("Filter2.doFilter.后");
  24.     }
  25. }

本文固定链接: http://www.devba.com/index.php/archives/1773.html | 开发吧

报歉!评论已关闭.