老师请问一下
多个过滤器的情况下:
过滤器的初始化(init)顺序以及销毁(destroy)顺序是什么?
我测试了一下
发现过滤器的初始化(init)顺序以及销毁(destroy)顺序跟在web.xml里注册的的顺序无关??
package 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 destroy() { System.out.println("Filter1 destroy"); } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("Filter1 init"); } }
package 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 destroy() { System.out.println("Filter2 destroy"); } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("Filter2 init"); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>FilterDemo2</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <filter> <filter-name>filter2</filter-name> <filter-class>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>filter.Filter1</filter-class> </filter> <filter-mapping> <filter-name>filter1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
请问过滤器的初始化(init)顺序以及销毁(destroy)顺序是根据什么来决定的?
1
收起
正在回答
2回答
同学你好。这个顺序是根据字母的优先顺序,在字母表中越靠前越优先。第一个字母相同时,默认以第二个字母顺序为准,比如:
如果解答了同学的疑问,望采纳~
祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星