淺談Listener、Filter、Servlet初始化順序

Listener、Filter、Servlet都有一個初始化的過程,對應的方法分別爲:java

contextInitialized(ServletContextEvent arg0); // Listener

init(FilterConfig filterConfig); // Filter

init(ServletConfig config); // Servlet

那麼它們的初始化順序是什麼呢?web

Listener > Filter > Servlettomcat

TestServlet.java:app

package com.king;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
   
public class TestServlet extends GenericServlet {     
    @Override    
    public void init(ServletConfig config){     
        System.out.println("Servlet 初始化 。。。");     
    }     
    @Override    
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {     
        System.out.println("Servlet service 。。。");     
    }     
}

TestFilter.java:ide

package com.king;     
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 TestFilter implements Filter{     
    public void init(FilterConfig filterConfig) throws ServletException {     
        System.out.println("Filter 初始化。。。");     
    }     
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {     
        System.out.println("doFilter 。。。");     
        chain.doFilter(request, response);     
    }     
    public void destroy() {     
        System.out.println("Filter 銷燬。。。");     
    }     
}

TestListener.java:url

package com.king;     
import javax.servlet.ServletContextEvent;     
import javax.servlet.ServletContextListener;     
    
public class TestListener implements ServletContextListener{     
    public void contextInitialized(ServletContextEvent arg0) {     
        System.out.println("Listener 初始化。。。");     
    }     
    public void contextDestroyed(ServletContextEvent arg0) {     
        System.out.println("Listener 銷燬。。。");     
    }     
}

web.xml:code

<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">    
    <servlet>    
        <servlet-name>s<servlet-name>    
        <servlet-class>com.cos.TestServlet<servlet-class>    
    <servlet>    
    <servlet-mapping>    
        <servlet-name>s<servlet-name>    
        <url-pattern>/login<url-pattern>    
    <servlet-mapping>    
    <filter>    
        <filter-name>f<filter-name>    
        <filter-class>com.cos.TestFilter<filter-class>    
    <filter>    
    <filter-mapping>    
        <filter-name>f<filter-name>    
        <url-pattern>/*<url-pattern>    
    <filter-mapping>    
    <listener>    
        <listener-class>com.cos.TestListener<listener-class>    
    <listener>    
<web-app>

啓動tomcat後,在控制檯裏以此打印出:xml

Listener 初始化。。。 Filter 初始化。。。 信息: Server startup in 675 msservlet

能夠看出Listener的初始化最先,Filter次之。他倆的初始化都是在容器啓動完成以前初始化的。Servlet沒有初始化,緣由是沒有匹配的請求進來。初始化的順序跟Listener、Filter、Servlet在web.xml中的順序無關。it

而多個Filter或多個Servlet的時候,誰的mapping在前面,誰先初始化。

相關文章
相關標籤/搜索