Initialization and invocation of listener, Filter, servlet in web development

Source: Internet
Author: User

In our use of the SPRING+SPRINGMVC development project, the general configuration in Web. XML is as follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appXmlns: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_3_0.xsd"version= "3.0">3     <!--Load Spring Configuration4 The default lookup configuration file location is: Web-inf/applicationcontext.xml. 5 The file location can be specified by the following parameters -6     <Context-param>  7         <Param-name>Contextconfiglocation</Param-name>  8         <Param-value>  9 Classpath:applicationContext.xmlTen         </Param-value> One     </Context-param> A      -     <Filter> -         <Filter-name>Corsfilter</Filter-name> the         <Filter-class>Com.filter.CORSFilter</Filter-class> -       </Filter> -       <filter-mapping> -         <Filter-name>Corsfilter</Filter-name> +         <Url-pattern>/*</Url-pattern> -       </filter-mapping> +        A     <Listener>   at         <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>   -     </Listener> -      -   <servlet> -     <Servlet-name>Spring-mvc</Servlet-name> -     <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> in       <!--Spring MVC Loads the configuration file as/web-inf/[servlet-name]-servlet.xml by default, or it can be specified with the following parameters - -     <!--<init-param> to <param-name>contextConfigLocation</param-name> + <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value> - </init-param> - the     <!--Load-on-startup is set to 1, which indicates that the project is loaded at startup - *     <Load-on-startup>1</Load-on-startup> $   </servlet>Panax Notoginseng </Web-app>
View Code

So when is the Contextinitialized method in listener called? The answer is in the Startinternal method of the Org.apache.catalina.core.StandardContext class

1 //Org.apache.catalina.core.StandardContext2 @Override3 protected synchronized voidStartinternal ()throwslifecycleexception {4     //omit part of the code, listing only the key code5 //Configure and Call application event listeners6             if(OK) {7                 if(!Listenerstart ()) {8Log.error (sm.getstring ("Standardcontext.listenerfail"));9OK =false;Ten                 } One             } A  -             Try { -                 //Start Manager theManager Manager =getmanagerinternal (); -                 if(Manager! =NULL) && (ManagerinstanceofLifecycle)) { - ( (Lifecycle) GetManager ()). Start (); -                 } +}Catch(Exception e) { -Log.error (sm.getstring ("Standardcontext.managerfail"), e); +OK =false; A             } at  -             //Configure and call application filters -             if(OK) { -                 if(!Filterstart ()) { -Log.error (sm.getstring ("Standardcontext.filterfail")); -OK =false; in                 } -             } to  +             //load and initialize all "load on startup" Servlets -             if(OK) { the                 if(!Loadonstartup (Findchildren ())) { *Log.error (sm.getstring ("Standardcontext.servletfail")); $OK =false;Panax Notoginseng                 } -}

Line 7th executes the trigger listener event, and theListenerstart () method has a listener.contextinitialized (event) statement, Call listener's Contextinitialized method directly.

The 27th line executes the filter's Init method, details please refer to the Tomcat source, not detailed in detail here.

Line 34th, if the servlet tag in Web. XML Specifies <load-on-startup>1</load-on-startup> (the value is greater than 0), the servlet instantiation is performed, Then call the servlet int (servletconfig config).

It is also possible to specify that when Tomcat starts, the order of the three calls is Listener-->filter-->servlet.

Note: The Startinternal method of the Standardcontext class is called when Tomcat starts.

Initialization and invocation of listener, Filter, servlet in web development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.