Java is a dynamically loaded and run language. This means that when an application holds the address of a class (CLASSPATH) and the name (package name and class name), it can load the class at any time during the program's run and create and use objects of that class. The servlet is based on this mechanism and the web container fused together. All of the currently known web containers that support Java Servlets are developed in Java. When the Web container receives the request information from the client, it finds the corresponding Servlet object in the servlet queue based on the Web Component address information in the URL, and if found it is used directly, loads the corresponding class if it is not found, and creates the object. In other words, the Servlet object was created the first time it was used, and once created it is reused and no new objects are created. All created servlet objects are garbage collected uniformly when the Web server stops running.
In order to resolve the problem of the correspondence between the client request address and the Java servlet, the Web container needs a file that describes the correspondence, typically the. xml file. If there are many servlets in a Web application, then XML will become very large. After the Servlet 3.0 specification was introduced, it was allowed to use declarative syntax in the servlet code instead of the descriptive information in Web. XML to make Web. xml lean down. is one of the processes.
In this diagram, we are simply outlining the relationship between the Web container and the servlet in a relatively easy-to-understand way, as well as the processing flow when the request is accepted. In the actual web container, this is a lot more complicated than this.
Listener, filter, servlet loading order and its explanation in Web. xml
One
1. When starting a Web project, the Web container reads its config file, XML, and reads <listener> and <context-param> two nodes.
2, emergency, allow to create a ServletContext (servlet context), all parts of this Web project will share this context.
3, the container will be <context-param> converted to a key value pair, and handed to ServletContext.
4. Create a listener for the class instance in the <listener> container.
Two
The Load-on-startup element specifies the order in which the servlet is loaded when the Web application is launched, and its value must be an integer. If its value is a negative integer or the element does not exist, the container will load the servlet when the servlet is called. If the value is a positive integer or 0, the container loads and initializes the servlet when configured, and the container must ensure that the value is loaded first. If the values are equal, the container can automatically select who to load first.
In the configuration of the servlet, the meaning of,<load-on-startup>5</load-on-startup> is:
Flags whether the container loads 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.
Three
The first thing to be sure is that 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: Listener, filter, servlet
There is also a configuration section: Context-param, which is used to provide ServletContext with key-value pairs, which are application context information. Our listener, filter, and so on are initialized with the information in these contexts, so should the Context-param configuration section be written before the Listener configuration section? In fact, the Context-param configuration section can be written anywhere, so the real load order is: Context-param, listener, filter, servlet
For a class of configuration sections, it is related to the order in which they appear. In the case of filter, it is possible to define multiple filter in Web. XML, a configuration section related to filter is filter-mapping, and it is important to note that for filter and Filter-mappi with the same filter-name NG configuration section, the filter-mapping must appear after the filter, or the corresponding filter-name is undefined when parsing to filter-mapping. When each filter is initialized when the Web container starts, it is initialized in the order in which the Filter configuration section appears, and when the request resource matches more than one filter-mapping, the filter interception resource is called sequentially in the order in which the Filter-mapping configuration section appears The DoFilter () method.
The servlet is similar to filter and is not mentioned here.
From this, it can be seen that the loading order of Web. XML is: Context-param---listener, filter--, and the order in which the actual program calls between the same type are in accordance with the order of the corresponding mapping Called by the line.
The relationship between the servlet and the Web container