一直没有仔细去研究下filter ,最近系统的测试了下:
先看代码吧
- FirstFilter.java
- ==================
- package com.test.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 FirstFilter implements Filter {
- @Override
- public void destroy() {
- }
- @Override
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- System.out.println("before invoke firstFilter's chain.doFilter() ..");
- chain.doFilter(request, response);
- System.out.println("after invoke firstFilter's chain.doFilter() ..");
- }
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- System.out.println("firstFilter init()...");
- }
- }
- ============
- SecondFilter.java
- ==============
- package com.test.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 SecondFilter implements Filter {
- @Override
- public void destroy() {
- }
- @Override
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- System.out.println("before invoke secondFilter's chain.doFilter() ..");
- chain.doFilter(request, response);
- System.out.println("after invoke secondFilter's chain.doFilter() ..");
- }
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- System.out.println("secondFilter init()...");
- }
- }
- ==========
- FirstServlet.java
- =============
- package com.test.servlet;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class FirstServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- System.out.println("servlet doGet be invoked...");
- req.getRequestDispatcher("test.jsp").forward(req, resp);
- }
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- doGet(req, resp);
- }
- }
- =========
- web.xml
- ===========
- <?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">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>firstFilter</filter-name>
- <filter-class>com.test.filter.FirstFilter</filter-class>
- </filter>
- <filter>
- <filter-name>secondFilter</filter-name>
- <filter-class>com.test.filter.SecondFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>secondFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>firstFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <servlet>
- <servlet-name>firstServlet</servlet-name>
- <servlet-class>com.alimama.servlet.FirstServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>firstServlet</servlet-name>
- <url-pattern>/firstServlet</url-pattern>
- </servlet-mapping>
- </web-app>
- ============
FirstFilter.java
==================
package com.test.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 FirstFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("before invoke firstFilter's chain.doFilter() ..");
chain.doFilter(request, response);
System.out.println("after invoke firstFilter's chain.doFilter() ..");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("firstFilter init()...");
}
}
============
SecondFilter.java
==============
package com.test.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 SecondFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("before invoke secondFilter's chain.doFilter() ..");
chain.doFilter(request, response);
System.out.println("after invoke secondFilter's chain.doFilter() ..");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("secondFilter init()...");
}
}
==========
FirstServlet.java
=============
package com.test.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("servlet doGet be invoked...");
req.getRequestDispatcher("test.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}
}
=========
web.xml
===========
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>firstFilter</filter-name>
<filter-class>com.test.filter.FirstFilter</filter-class>
</filter>
<filter>
<filter-name>secondFilter</filter-name>
<filter-class>com.test.filter.SecondFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>secondFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>firstFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>firstServlet</servlet-name>
<servlet-class>com.alimama.servlet.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstServlet</servlet-name>
<url-pattern>/firstServlet</url-pattern>
</servlet-mapping>
</web-app>
============
然后发布,发现打印的日志如下:
。。。
firstFilter init()...
secondFilter init()...
。。。
2008-10-23 11:36:43 org.apache.catalina.startup.Catalina start
信息: Server startup in 3665 ms
这里过滤器初始化好了。
当我们访问我们的 应用:http://127.0.0.1:8080/appName
发现打印日记如下:
before invoke secondFilter's chain.doFilter() ..
before invoke firstFilter's chain.doFilter() ..
after invoke firstFilter's chain.doFilter() ..
after invoke secondFilter's chain.doFilter() ..
当我们将web.xml中filter的位置进行调整后:
- <?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">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>firstFilter</filter-name>
- <filter-class>com.test.filter.FirstFilter</filter-class>
- </filter>
- <filter>
- <filter-name>secondFilter</filter-name>
- <filter-class>com.test.filter.SecondFilter</filter-class>
- </filter>
- <SPAN style="COLOR: #ff0000"> <filter-mapping>
- <filter-name>firstFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>secondFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping></SPAN>
- <servlet>
- <servlet-name>firstServlet</servlet-name>
- <servlet-class>com.alimama.servlet.FirstServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>firstServlet</servlet-name>
- <url-pattern>/firstServlet</url-pattern>
- </servlet-mapping>
- </web-app>
然后在启动应用,会看到打印:
before invoke firstFilter's chain.doFilter() ..
before invoke secondFilter's chain.doFilter() ..
after invoke secondFilter's chain.doFilter() ..
after invoke firstFilter's chain.doFilter() ..
总结:
filter的执行顺序是按照web.xml中定义的顺序进行执行的(当多个filter都匹配的时候),执行顺序如下图表示。