涉及的类: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链。验证
<filter>
<filter-name>filter2</filter-name>
<filter-class>filterweb.tony.filter.Filter2</filter-class>
</filter>
<filter-mapping>
<filter-name>filter2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>filter1</filter-name>
<filter-class>filterweb.tony.filter.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
浏览器中请求http://127.0.0.1:9090//filterweb/index.jsp,服务器发现web.xml中filter1和filter2都会对此路径过滤,按照顺序形成filterchain:filter2----->filter1。控制台输出信息:
Filter2.doFilter.前
Filter1.doFilter.前
Filter1.doFilter.后
Filter2.doFilter.后
将web.xml中filter的顺序反过来
<filter>
<filter-name>filter1</filter-name>
<filter-class>filterweb.tony.filter.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>filter2</filter-name>
<filter-class>filterweb.tony.filter.Filter2</filter-class>
</filter>
<filter-mapping>
<filter-name>filter2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
浏览器中请求http://127.0.0.1:9090//filterweb/index.jsp,服务器发现web.xml中filter1和filter2都会对此路径过滤,按照顺序形成filterchain:filter2----->filter1。控制台输出信息:
Filter1.doFilter.前
Filter2.doFilter.前
Filter2.doFilter.后
Filter1.doFilter.后
(2)根据请求的资源路径选择filter。例如现在将filter1的过滤路径设置为/common/*,即common下的所有资源。
<filter>
<filter-name>filter1</filter-name>
<filter-class>filterweb.tony.filter.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/common/*</url-pattern>
</filter-mapping>
并在common目录下新建一个common_index.jsp。
浏览器中请求http://127.0.0.1:9090//filterweb/index.jsp,服务器发现web.xml中只有filter2会对此路径过滤。控制台输出信息
Filter2.doFilter.前
Filter2.doFilter.后
浏览器中请求http://127.0.0.1:9090//filterweb/common/common_index.jsp,服务器发现web.xml中filter1和filter2会对此路径过滤。控制台输出信息
Filter2.doFilter.前
Filter1.doFilter.前
Filter1.doFilter.后
Filter2.doFilter.后
(3)执行顺序:【filter1的dofilter()前------>filter1的dofilter()】======>【filter2的dofilter()前------>filter2的dofilter()------>filter2的dofilter()后】======>【filter1的dofilter()后】
可以从(1)的结果中看出来
filterweb.tony.filter.Filter1.java
package filterweb.tony.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Filter1 implements Filter {
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("Filter1.init");
}
@Override
public void destroy() {
System.out.println("Filter1.destroy");
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
System.out.println("Filter1.doFilter.前");
chain.doFilter(req, resp);
System.out.println("Filter1.doFilter.后");
}
}
package filterweb.tony.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Filter2 implements Filter {
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("Filter2.init");
}
@Override
public void destroy() {
System.out.println("Filter2.destroy");
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
System.out.println("Filter2.doFilter.前");
chain.doFilter(req, resp);
System.out.println("Filter2.doFilter.后");
}
}
================================================================
原创文章,转载请注明链接