Why load-on-startup is used in web.xml

Recently I was creating servlet based web application. Whenever I deployed my web application and requested the first time to get the number of products from the database; the response time was bit high as compared to the subsequent request.

This made me dig out what actually happens when the first request arrives to web server/container. After going through many technical forums, I understood about the way servlet is loaded by the container when the first request arrives at the container. So whenever the first request arrives at the server it loads the servlet. Because of this my first request was taking more time as compared to subsequent requests.

Now the question was how to pre-load servlet in the container or is there a way to load servlet on server startup? 

Load On Startup

The answer is YES. The servlet can be loaded at the time of server startup by using <load-on-startup> element in web.xml file.

Let us understand how to use load-on-startup is used in web.xml to pre-initialize any servlet.

Defining load-on-startup in web.xml

The <servlet> element defined in web.xml file can have a subelement called <load-on-startup> which you can be used to specify when the servlet container should load the servlet. If <load-on-startup> element is not defined, then servlet container will typically load your servlet when the first request comes for it.

Hence if you specify <load-on-startup> element, servlet container will load the servlet when the servlet container starts. Let us see how it is defined in web.xml file with example.

<servlet>
    <servlet-name>AuthenticationServlet</servlet-name>
    <servlet-class>com.modernpathshala.AuthenticationServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

Similarly, you can specify load-on-startup for other servlets also.

What if two servlets have different value of load-on-startup element?

If two servlets have different value of load-on-startup element then the priority is given to servlet having smaller value of load-on-startup for being loaded by container. for example:

<servlet>
  <servlet-name>AuthenticationServlet</servlet-name>
  <display-name>AuthenticationServlet</display-name>
  <servlet-class>com.modernpathshala.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.modernpathshala.AuthorizationServlet</servlet-class>
  <load-on-startup>3</load-on-startup>
</servlet>

In above example, AuthenticationServlet will be loaded first as it has smaller value of load-on-startup element.

What if two servlets have the same value of load-on-startup element?

If two servlets have same value of load-on-startup element then servlets are loaded in a sequence they are defined in web.xml file

<servlet>
  <servlet-name>AuthenticationServlet</servlet-name>
  <display-name>AuthenticationServlet</display-name>
  <servlet-class>com.modernpathshala.AuthenticationServlet</servlet-class>         
  <load-on-startup>3</load-on-startup>
</servlet>

<servlet>
  <servlet-name>AuthorizationServlet</servlet-name>
  <display-name>AuthorizationServlet</display-name>
  <servlet-class>com.modernpathshala.AuthorizationServlet</servlet-class>
  <load-on-startup>3</load-on-startup>
</servlet>

In above example, AuthenticationServlet will be loaded first as it is defined first in web.xml file.

So in this article we learned how to load any servlet at the time of server startup which can improve the response time of the first request served.

Article tagged as
Author
Author: Amit Gupta
Published On: 22/11/2015
Last revised On: 22/11/2015
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media