Monday, July 26, 2021

What is load-on-startup servlet element in web.xml with Example?

load-on-startup is an element that appears inside <servlet> tag in web.xml.4 years back load-on-startup was a very popular servlet interview question because not many Java J2EE developer was familiar with this element and how load-on-startup works inside a servlet container like tomcat or WebSphere. In this J2EE Tutorial, we will see what is a load on startup, how to use the load-on-startup element, and what are different values we can configure for load-On-Startup inside web.xml.

What is load-on-startup tag in Servlet?

load on startup servlet tag exampleAs stated earlier load-on-startup is a tag element that appears inside <servlet> tag in web.xml. load-on-startup tells the web container about the loading of a particular servlet. 

If you don't specify load-on-startup then the container will load a particular servlet when it feels necessary most likely when the first request for that servlet will come, this may lead to a longer response time for that query if Servlet is making database connections or performing LDAP authentication which contributes network latency or any other time-consuming job, to avoid this, web container provides you a mean to specify certain servlet to be loaded during deployment time of application by using the load-on-startup parameter.

If you specify the load-on-startup parameter inside a servlet then based upon its value Container will load it. you can specify any value to this element but in the case of load-on-startup>0, a servlet with less number will be loaded first. 

For example, in the below web.xml AuthenticationServlet will be loaded before AuthorizationServlet because the load-on-startup value for AuthenticationServlet is less (2) while for AuthorizationServlet is 4.



load-on-startup Example in web.xml

here is an example of how to use load on startup tag inside servlet element in web.xml:

<servlet>
<servlet-name>AuthenticationServlet</servlet-name>
<display-name>AuthenticationServlet</display-name>
<servlet-class>com.trading.AuthenticationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet>
<servlet-name>AuthorizationServlet</servlet-name>
<display-name>AuthorizationServlet</display-name>
<servlet-class>com.trading.AuthorizationServlet</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>

Important points on the load-on-startup element

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

When to use <load-on-startup> in web.xml

<load-on-startup> is suitable for those servlets which perform time-consuming jobs e.g. Creating a Database Connection pool, downloading files or data from the network, or prepare an environment ready for servicing clients in terms of initializing cache, clearing pipelines, and loading important data in memory. 

If any of your servlets perform these jobs then declare them using <load-on-startup> element and specify order as per your business logic or what suites your application. 

Remember to lower the value of <load-on-startup>, the servlet will be loaded first. You can also check your web container documentation on how exactly the load on start-up is supported.


That’s all on the load-on-startup tag of the servlet element in web.xml. Use it carefully and it can reduce response time for your web application. You can also check my Struts interview questions and spring interview questions for more on the J2EE interview.


Related Tutorials

9 comments :

விஜய்கரன் பா said...

very useful and detailed explanation.

Rakesh said...

A good example of load on startup is spring's dispatcher servlet which is always specified with load-on-startup tag value:
dispatcher org.springframework.web.servlet.DispatcherServlet 2 </servlet

Javin @ Servelt Interview Questions said...

It looks like blogger ate < tag , anyway I don't think you can put Servlet element inside another servlet element, you got to check web.xml DTD which says what can go inside servlet tag. but most probably answer is no.

Diyyana Kishore Babu said...

Diyyana Kishore Babu:-

Hi Javin, one small add on point to ‘’. When web container started and while running, it loads the deployed web application, during this web container creates the servlet context object based on the information provided in web.xml file. Once this provided for the particular servlet class it give the priority order to this sequence defined servlet classes to load first then subsequent servlet classes has to load. For instance this is useful during context loading if there are no of frameworks developed based on servelts (Struts/JSF/Spring-MVC etc..), which has to load first and next during their dependencies at runtime.

Anonymous said...

Hi Javin,
You have mentioned that if its value is greater than 0 then only in start up it will be initialized. I think its greater than and equal to 0. Please confirm.

Thanks,
Sukhi.

javin paul said...

@Anonymous, Yes, you are right.

1) Container will intialize the servlet and call its init() method if load-on-statup is 0 or a positive integer greater than zero, when application is deployed or when servlet container is started and application is already deployed.

2) load-on-startup is actually eager loading and can also used to define relative ordering e.g. servlets with lower load-on-startup will start first e.g. servlet with load-on-startup = 1 will start before a servlet with load on startup = 2

3) for servlets with same load-on-startup value, container is free to initialize them in any relative order.

Rajesh said...

what is the max value we can pass to this tag

Unknown said...

There is no max value we can pass any number

javin paul said...

Hello @Unknown, yes, you can pass any number, no max value.

Post a Comment