Load on Startup in Servlet

Load on Startup in Servlet. When we enable <load-on-startup> on servlet then the Servlet Container creates our Servlet Class object either during Server startup or during the deployment of the web application. This is called pre-instantiation and pre-initialization of the servlet.

Example of Load on Startup in servlet:-

<web-app>
  <servlet>
    <servlet-name>date</servlet-name>
    <servlet-class>com.know.program.DateServlet</servlet-class>
    <load-on-startup>1<load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>date</servlet-name>
    <url-pattern>/date</url-pattern>
  </servlet-mapping>
</web-app>

Advantage of enabling <load-on-startup> on servlet component

First, we will discuss what happens when the load on startup is not enabled? In this situation when the first request is given to the servlet component then the servlet participates in servlet loading, servlet instantiation, servlet initialization, and request processing. But other than the first request, the servlet directly participates in request processing. Due to this reason, the response time of the first request is a little high when compared to the response time of the other than the first request. 

To reduce the response time of the first request and equalize with other the first request we need to make Servlet Container performing servlet loading, instantiation, initialization either during server startup or during the deployment of the web application. For this purpose, we need to enable the <load-on-startup> on the servlet. 

  • It is not mandatory to enable <load-on-startup> on every Servlet Component of the web application. 
  • We should enable <load-on-startup> on such Servlet Components which will be requested immediately after deployment, for example:- Servlet Component giving main menu/home page.

When the servlet container creates a Servlet Class object?

There are two cases when a servlet container creates a Servlet Class object.

Case1:- If <load-on-startup> is not enabled.

  • When the first request is given to that servlet component.
  • When the first request is given to servlet after restarting web application or restarting web server or reloading of the web application or redeploying of the web application.

If the load on startup is not enabled on the servlet component then the servlet container creates a servlet class object only when it gets the first request to the servlet, it is called late or lazy instantiation of the servlet.

Case2:- If <load-on-startup> is enabled.

  • Then either during startup or during the deployment of the web application.

When the load-on-startup is enabled on the servlet component then the servlet container creates our servlet class object either during startup or during the deployment of the web application, it is called pre-initialization or early initialization of servlet.

When the Servlet Containers destroy servlet class objects?

The Servlet Containers destroy servlet class object in the following cases:-

  • When the web application is stopped/reloaded/undeployed.
  • When the server is stopped/crashed.
  • If the Servlet Class object is continuously idle for a long time.

Note:- The Servlet Container does not give any special priority towards the destruction of our servlet Class object even through the <load-on-startup> is enabled on it.

<load-on-startup> Priority Value

If multiple Servlet components of a web application are enabled with <load-on-startup> then their order of pre-instantiation and pre-initialization will be decided based on the <load-on-startup> priority value.

<load-on-startup>1</load-on-startup>

Here 1 is the load-on-startup priority value. The load-on-priority value can be positive, negative, or zero.

  • A high positive priority value indicates low priority. 
  • A low positive priority value indicates high priority. 
  • The negative priority value will be ignored as a load-on-startup priority value. Giving priority values as negative is equal to not enabling the load-on-startup.

For better understanding purposes, assume the priority value as a ranking system, where 1 has higher priority than 2.

Servlet Priority valueInstantiation order
Servlet110fourth
Servlet21second
Servlet3-5Ignored
Servlet40first
Servlet5-10Ignored
Servlet62third
Servlet720fifth

Example of web.xml file having load-on-startup enabled on multiple servlet,

<web-app>
  <servlet>
    <servlet-name>lc</servlet-name>
    <servlet-class>com.know.program.LCTestServlet</servlet-class>
    <load-on-startup>10<load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>lc</servlet-name>
    <url-pattern>/lctest</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>plain</servlet-name>
    <servlet-class>com.know.program.PlainServlet</servlet-class>
    <load-on-startup>0<load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>plain</servlet-name>
    <url-pattern>/plain</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>html</servlet-name>
    <servlet-class>com.know.program.HTMLServlet</servlet-class>
    <load-on-startup>-5<load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>html</servlet-name>
    <url-pattern>/html</url-pattern>
  </servlet-mapping>
</web-app>

In the tomcat server, after Tomcat version 8 onwards empty load-on-startup without priority value will generate an error.

Enabling <load-on-startup> on servlet with having negative value as the priority value is equal to not enabling <load-on-startup> on Servlet.

What happens when all servlet components have the same priority order?

In this situation, all the servlet components will be treated as without load-on-startup priority value and the object of those servlet Classes will be created when the first request is given. It means Servlet Container treats them as <load-on-startup> is not enabled.

What will be the Servlet Class loading order when more than one servlet component has the same priority?

In this situation First come first serve. In order they are placed in the web.xml file, in that order servlet class will be loaded.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.