Summary of the loading sequence of Java Web. xml nodes during file loading

Source: Internet
Author: User

Web. xml loading process (STEP ):


1. When a web project is started, the container (such as Tomcat) will read its configuration file web. xml. To read two nodes:

<Listener> </listener> and <context-param> </context-param>

 
 

2. Then, the container creates a servletcontext, and all parts of the web project share the context.

3. The Container converts <context-param> </context-param> to a key-Value Pair and submits it to servletcontext.

4. Create a class instance in the <listener> </listener> container, that is, create a listener.

5. The contextinitialized (servletcontextevent ARGs) initialization method will be available in the listener. In this method, obtain:

 

 

Servletcontext = Servletcontextevent. getservletcontext ();
Context
- Param Value = Servletcontext. getinitparameter ( " Context-Param key " );

 

 

6. after obtaining the value of context-Param, you can perform some operations. note that your web project has not been fully started yet. this action will be earlier than all servlets.
In other words, the operations you perform on the key value in <context-param> will be executed before your web project is fully started.

7. For example, you may want to open the database before the project starts.
Then, you can set the database connection method in <context-param> and initialize the database connection in the listener class.

8. This listener is a self-written class. In addition to the initialization method, this listener also has the destruction method. It is used to release resources before closing the application. For example, the database connection is closed.

 

Web. xml node loading sequence:

 

It is certain that the node loading sequence is irrelevant to their order in the web. xml file. That is, the filter will not be loaded first because the filter is written before the listener. The final conclusion is: listener-> filter-> Servlet

There is also a configuration node: Context-Param, which is used to provide a key-value pair to servletcontext, that is, the applicationProgramContext information. Our listener and filter will use the information in these contexts during initialization. Should the context-Param configuration section be written before the Listener Configuration section? In fact, the context-Param configuration section can be written in any location, so the actual loading order is:

Context-param-> listener-> filter-> Servlet

For certain configuration sections, the order in which they appear is related. Take filter as an example. of course, multiple filters can be defined in XML. One configuration section related to the filter is filter-mapping. Note that, in the filter and filter-mapping configuration sections with the same filter-name, the filter-mapping must appear after the filter; otherwise, when the filter-mapping is parsed, the filter-name corresponding to it is not defined yet. When a Web container is started, each filter is initialized according to the sequence in the filter configuration section. When the requested resource matches multiple Filters-mapping, filter intercepts resources by calling the dofilter () method in sequence in the filter-mapping configuration section.

Servlet is similar to filter. We will not repeat it here.

[Load Spring]

For example, if the filter requires bean, but the loading order is: load the filter first and then load spring, then the bean in the initialization operation of the filter is null;

Therefore, if bean is used in the filter, you can change spring loading to listener:

 

< Listener >
< Listener-class >
Org. springframework. Web. Context. contextloaderlistener
</ Listener-class >
</ Listener >

 

 

Conclusion:

Web. the XML loading sequence is: [context-param-> listener-> filter-> servlet-> spring], the actual Program Calling sequence between nodes of the same type is called according to the corresponding Mapping Sequence.

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.