Web container launch--web.xml loading explained

Source: Internet
Author: User

Recently in the spring source code, about the Web. xml file in the container (Tomcat, JBoss, etc.) load order problem is very confusing, through the collection of data, the following conclusions:

1. The order of loading is independent of their order in the Web. xml file. That is, the filter is not loaded before the filter is written in front of the listener. The final conclusion is: Context-param, listener, filter, and servlet;

    • The container will first go to its config file, Web. Xml to read two nodes : <context-param></context-param>; It is used to provide ServletContext with key-value pairs (that is, application context information, listener, filter, etc.) that are used to initialize the information in these contexts.
    • The container creates a class instance in <listener></listener>, creates a listener based on the configured class Classpath <listener-class>, and in the listening Contextinitialized (Servletcontextevent args) initialization method (which implements the Servletcontextlistener interface), when the Web app is started, the system calls Listener the method, in this method, get the key-value pairs in the ServletContext:

ServletContext application =servletcontextevent.getservletcontext ();

The value of String context-param = application.getinitparameter ("Key of Context-param ");

    • The container reads <filter></filter> Instantiates the filter according to the specified classpath,<filter-mapping></filter-mapping> Used to define the URL to which the filter corresponds
    • The same elements appear in Web. XML and are loaded according to the order in which they appear in the configuration file Take the filter (servlet-like filter) as an example: in Web. XML, you can certainly define multiple filter, a configuration section related to filter is filter-mapping, it is important to note that for the same In the case of the filter and filter-mapping configuration sections of Filter-name, filter-mapping must appear after the filter, otherwise the filter-name that it corresponds to is undefined when parsing to filter-mapping. When each filter is initialized at the start of the Web container, it is initialized in the order in which the Filter configuration section appears, and when the request resource matches multiple filter-mapping, the filter interception resource is invoked in the order in which the Filter-mapping configuration section appears in sequence D The Ofilter () method.

2. in the servlet configuration, the meaning of,<load-on-startup>1</load-on-startup> is: Does the tag container load the servlet when it is started?

A value of 0 or greater than 0 o'clock indicates that the container loads the servlet when the app is started;

When it is a negative number or is not specified, it indicates that the container is loaded only when the servlet is selected.

The lower the value of a positive number, the higher the priority to start the servlet.

Web container launch--web.xml loading explained

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.