The role of the servlet configuration load-on-startup parameter

 

In the servlet configuration, the meaning of <load-on-startup>1</load-on-startup> is to mark whether the container loads the servlet when it starts.

When the value is >= 0, it means that the container loads the servlet when the application starts;

When a negative number or not specified, instructs the container to load only when the servlet is selected.

When the value >= 0, that is, the servlet is loaded when the application starts, the smaller the value, the higher the priority of starting the servlet.

  If we set up multiple servlets in web.xml, we can use load-on-startup to specify the loading order of servlets, and the server will initialize the servlets in turn according to the size of load-on-startup. If the load-on-startup value of multiple servlets is repeated, the load order container decides not to load in the order in the document

 
After configuring load-on-startup, the servlet loads immediately after startup, but only calls the servlet's init() method to initialize the servlet-related resources. After the initialization is successful, the servlet can respond to the web request; if the load-on-startup is not configured, the container generally detects whether the servlet is initialized when it responds to the web request for the first time. If it is not initialized, the init() of the servlet is called. Initialize first, and then respond to the request after the initialization is successful.

PS: Generally, when we develop web applications, we will configure this parameter. There are two advantages: 1. If the initialization process fails, the container will prompt that the startup failed. At this time, we can know the relevant errors in advance; 2. Configuring this parameter is equivalent to The work of initializing the servlet is transferred to the container startup process, so that as long as the container is successfully started, it can immediately respond to web requests.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326346459&siteId=291194637