Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Tomcat initializes the servlet container after parsing the server.xml

Shulou Source: shulou.com Published: 2022-06-02 04:40:37 10月04日 Update

This article introduces the knowledge of "the process of initializing the servlet container after tomcat parses server.xml". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Learning background

After configuring server.xml, tomcat parses server.xml and initializes the servlet container. We learned the startup process of tomcat earlier:

Https://www.lixiang.red/articles/2019/08/07/1565188609044.html

Start method of Catalina

Following in yesterday's footsteps, after the init of Bootstrap, the start method of Bootstrap is called

Public void start () throws Exception {if (catalinaDaemon==null) {init ();} / / use reflection to call Catalina's start method Method method = catalinaDaemon.getClass (). GetMethod ("start", (Class []) null); method.invoke (catalinaDaemon, (Object []) null);}

As shown above: reflect again to call the start method of Catalina.java

At the beginning of the method, the load method is called to load the

If (getServer () = = null) {load ();} load initialization Digester related information

First, set some runtime properties in initNaming and insert the knowledge points of ENV and Property.

ENV and Propertypublic class EnvStudy {public static void main (String [] args) {Map getenv = System.getenv (); System.out.println (getenv); Properties properties = System.getProperties (); System.out.println (properties);}}

ENV is the environment variable of the computer

Properties are some of the properties of the program at run time

Initialize digester / / Before digester-it may be needed initNaming ()

/ / Create and execute our Digester / / configure the format of server.xml and the parsing rule Digester digester = createStartDigester () in createStartDigester

The code section of createStartDigester is as follows:

By comparing the previous load method, we can see two timing methods

/ / this method is specially used for timing, and its time is not related to real life time, long T1 = System.nanoTime (); / / this method is related to the operating system and corresponds to our real life time long t1=System.currentTimeMillis ()

By looking for familiar elements, we find that digester and server.xml correspond one to one.

Digester.addObjectCreate ("Server", "org.apache.catalina.core.StandardServer", "className")

What the above method represents is:

When matching to the Server node, take className to create the Object, and if the set className is empty, use StandardServer to create the Object

Stack structure of digester-top stack GlobalNamingResources-Server-Catalina-digester.addSetNext ("Server/GlobalNamingResources", "setGlobalNamingResources", "org.apache.catalina.deploy.NamingResourcesImpl")

Find the element Server at the top of the secondary stack, call its setGlobalNamingResources method, and pass the top element GlobalNamingResources as a parameter to the above method

Start the Servlet container

After a series of inputstream null judgments, begin to enter the real start-up process.

/ / use the digester rules defined above to parse server.xml inputSource.setByteStream (inputStream), digester.push (this), digester.parse (inputSource)

This is the end of the content of "tomcat initializes the servlet container after parsing server.xml". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Methods processes containers elements time knowledge learning content such as above attributes more reality rules reflection life operation configuration practicality one-to-one correspondence Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple Linux NVidia MySQL