web三大组件的加载顺序
Web三大组件:过滤器组件 监听器组件 Servlet组件
过滤器的顶级接口:javax.servlet.Filter
监听器的顶级接口:javax.servlet.ServletContextListener
Servlet的顶级接口:javax.servlet.Servlet
测试如下:
MyServlet:
package web; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class MyServlet implements Servlet { public void destroy() { System.out.println("Servelt销毁了"); } public ServletConfig getServletConfig() { return null; } public String getServletInfo() { return null; } public void init(ServletConfig arg0) throws ServletException { System.out.println("Servelt加载了"); } public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { } }
MyFilter:
package web; 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 MyFilter implements Filter { public void destroy() { System.out.println("Filter销毁了"); } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { } public void init(FilterConfig config) throws ServletException { System.out.println("Filter加载了"); } }
MyListener:
package web; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent arg0) { System.out.println("Listener销毁了"); } public void contextInitialized(ServletContextEvent arg0) { System.out.println("Listener加载了"); } }
接着需要在web.xml文件中配置这些组件,注意看web.xml文件中的配置顺序:
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>loadOrder</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 过滤器--> <filter> <display-name>MyFilter</display-name> <filter-name>MyFilter</filter-name> <filter-class>web.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/MyFilter</url-pattern> </filter-mapping> <!-- Servlet组件 --> <servlet> <description></description> <display-name>MyServlet</display-name> <servlet-name>MyServlet</servlet-name> <servlet-class>web.MyServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> <!-- 监听器 --> <listener> <listener-class>web.MyListener</listener-class> </listener> </web-app>
按照如上配置之后:
当启动服务器后其结果为:
Listener加载了
Filter加载了
Servelt加载了
总结:所以三大组件的加载顺序与配置文件的顺序无关,其加载顺序为:Listener>Filter>Servlet