FAQ

Java

JSP

Servlet


Advertisement



What is <load-on-startup> in web.xml file?

The <load-on-startup> is a tag element which appear inside <servlet> tag in a web.xml. This tag specifies that the servlet should be loaded automatically on the startup of the web application.

<web-app ...>
    ...
    <servlet> 
        <servlet-name>ServletOne</servlet-name> 
        <servlet-class>com.xyzws.servlets.ServletExampleOne</servlet-class>
        <description>This servlet is an example servlet</description>
        <init-param> 
            <param-name>debug</param-name> 
            <param-value>true</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet> 
        <servlet-name>ServletTwo</servlet-name> 
        <servlet-class>com.xyzws.servlets.ServletTwo</servlet-class>
        <description>This servlet is an example servlet</description>
        <init-param> 
            <param-name>debug</param-name> 
            <param-value>true</param-value> 
        </init-param> 
        <load-on-startup>2</load-on-startup> 
    </servlet> 
    ...
</web-app>

The <load-on-startup> value is a positive integer or 0 which specifies the servelts loaded order as per your business logic or what suites your application. The container must guarantee that servlets with lower values are loaded before servlets with higher values. For above web.xml, the servlet ServletOne is loaded before the servlet ServletTow. If two or more servlets have the same <load-on-startup> positive integer value then they will be loaded in an order on which they are declared inside web.xml file.

When the servlet is loaded by container, its init() method is called. You can do the following in the init() method:

  • You can start any daemon threads (Spawn threads are explicily forbidden by the J2EE standard. If you do create threads, remember to call thread.setDaemon(true).)
  • You can perform initialisation of the application, such as creating database connection, parsing a settings file or reading data from network to preloading data into memory

If <load-on-startup> is negative integer or the element is not presented then servlets will be loaded when the container decides it needs to be loaded - typically on it's first access. This is suitable for servlets that don't need to perform special initialisation.

You should also check your web container documentation on how exactly <load-on-startup> is supported.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |