22 Apr 2015

What is Servlet load-on-startup in web.xml

Generally after deploying an application servlet is loaded on first request, so it takes more time to process the first request. In order to overcome it we can use load-on-startup element inside deployment descriptor (web.xml). If you specify the load-on-startup element in web.xml, servlet will be loaded at server start up time and thus it will take less time for processing first request.

Let's see how to configure this element with an example.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
   <servlet>
      <servlet-name>MyServlet1</servlet-name>
      <servlet-class>com.j2eekart.servlet.Servlet1</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet>
      <servlet-name>MyServlet2</servlet-name>
      <servlet-class>com.j2eekart.servlet.Servlet2</servlet-class>
      <load-on-startup>2</load-on-startup>
   </servlet>
</web-app>
As shown load-on-startup element holds an integer value and:
  1. If the value if 0 or negative then servlet will be loaded on request time i.e., on first request only.
  2. If the value is greater than 0 then servlet will be loaded on server start up and calls to init() method of servlet by web container.
  3. If there are multiple servlets defined with load-on-startup value then they will be loaded in the order declared inside web.xml.
  4. If there are more than one servlet defined with same load-on-startup value then they will be loaded in the order declared inside web.xml.





Popular Posts

Write to Us
Name
Email
Message