[jakarta-ee] What does the servlet <load-on-startup> value signify

I am getting a bit confused here. In our application we are having a few servlets defined. Here is the excerpt from the web.xml for one of the servlets:

<servlet>
    <servlet-name>AxisServlet</servlet-name>
    <display-name>Apache-Axis Servlet</display-name>
    <servlet-class>com.foo.framework.axis2.http.FrameworkServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet> 

As per my understanding the value for the <load-on-startup> has to be a positive integer in order for it to get loaded automatically. I looked up on google but the responses I came across only added to my confusion.

This question is related to jakarta-ee servlets web.xml

The answer is


  1. If value is same for two servlet than they will be loaded in an order on which they are declared inside web.xml file.
  2. if is 0 or negative integer than Servlet will be loaded when Container feels to load them.
  3. guarantees loading, initialization and call to init() method of servlet by web container.
  4. If there is no element for any servlet than they will be loaded when web container decides to load them.

Servlet Life Cycle

The lifecycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.

  1. If an instance of the servlet does not exist, the web container:

    a. Loads the servlet class

    b. Creates an instance of the servlet class

    c. Initializes the servlet instance by calling the init method (initialization is covered in Creating and Initializing a Servlet)

  2. The container invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods.

A 0 value on load-on-startup means that point 1 is executed when a request comes to that servlet. Other values means that point 1 is executed at container startup.


--> (Absence of load-on-start-up) tag First of all when ever servlet is deployed in the server, It is the responsibility of the server to creates the servlet object. Eg: Suppose Servlet is deployed in the server ,(Servlet Object is not available in server) client sends the request to the servlet for the first time then server creates the servlet object with help of default constructor and immediately calls init() . From that when ever client sends the request only service method will get executed as object is already available

If load-on-start-up tag is used in deployment descriptor: At the time of deployment itself the server creates the servlet object for the servlets based on the positive value provided in between the tags. The Creation of objects for the servlet classes will follow from 0-128 0 number servlet will be created first and followed by other numbers.

If we provide same value for two servlets in web.xml then creation of objects will be done based on the position of classes in web.xml also varies from server to server.

If we provide negative value in between the load on start up tag then server wont create the servlet object.

Other Scenarios where server creates the object for servlet.

If we dont use load on start up tag in web.xml, then project is deployed when ever client sends the request for the first time server creates the object and server is responsible for calling its life cycle methods. Then if a .class is been modified in the server(tomcat). again client sends the request for modified servlet but in case of tomcat new object will not created and server make use of existing object unless restart of server takes place. But in class of web-logic when ever .class file is modified in the server with out restarting the server if it receives a request then server calls the destroy method on existing servlet and creates a new servlet object and calls init() for its initilization.


If the value is <0, the serlet is instantiated when the request comes, else >=0 the container will load in the increasing order of the values. if 2 or more servlets have the same value, then the order of the servlets declared in the web.xml.


Short Answer: value >= 0 means that the servlet is loaded when the web-app is deployed or when the server starts. value < 0 : servlet is loaded whenever the container feels like.

Long answer (from the spec):

The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive 128 integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.


It indicates that the servlet won't be started until a request tries to access it.

If load-on-startup is greater than or equal to zero then when the container starts it will start that servlet in ascending order of the load on startup value you put there (ie 0, 1 then 2 then 5 then 10 and so on).


It is simple as you don't even expect.

If the value is positive it loaded when the container starts

If the value is not positive than the servelet is loaded when the request is made.


yes it can have same value....the reason for giving numbers to load-on-startup is to define a sequence for server to load all the servlet. servlet with 0 load-on-startup value will load first and servlet with value 1 will load after that.

if two servlets will have the same value for load-on-startup than it will be loaded how they are declared in the web.xml from top to bottom. the servlet which comes first in the web.xml will be loaded first and the other will be loaded after that.


As stated on other answer and this load-on-startup article zero is acceptable and in the absent of any other servlet this will take priority on loading and loaded during deployment. Best use of load-on statup is to load servlets which takes longer time to initialize well before first request come like servlets which creates connection pool or make network call or hold bulky resource, this will significantly reduce response time for first few request.


The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the attribute "load-on-startup" in "web.xml" file. If the attribute has a positive integer(0 to 128) then the servlet is load with the loading of the container otherwise it loads when the first request comes for service.

When the servlet is loaded once it gets request then it is called "Lazy loading".


Examples related to jakarta-ee

Java 11 package javax.xml.bind does not exist javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) web.xml is missing and <failOnMissingWebXml> is set to true WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default Name [jdbc/mydb] is not bound in this Context An internal error occurred during: "Updating Maven Project". java.lang.NullPointerException How to consume a SOAP web service in Java java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

Examples related to servlets

Google Recaptcha v3 example demo Difference between request.getSession() and request.getSession(true) init-param and context-param java.lang.NoClassDefFoundError: org/json/JSONObject how to fix Cannot call sendRedirect() after the response has been committed? getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? Create a simple Login page using eclipse and mysql Spring get current ApplicationContext insert data into database using servlet and jsp in eclipse What is WEB-INF used for in a Java EE web application?

Examples related to web.xml

SEVERE: ContainerBase.addChild: start:org.apache.catalina.LifecycleException: Failed to start error Session TimeOut in web.xml How to configure welcome file list in web.xml What is the significance of url-pattern in web.xml and how to configure servlet? No WebApplicationContext found: no ContextLoaderListener registered? How to specify the default error page in web.xml? How to define servlet filter order of execution using annotations in WAR Loading context in Spring using web.xml Could not open ServletContext resource Why do we use web.xml?