Life cycle of servlet from the perspective of the container
1. After start, the container is looking for servlet classes in the appropriate directory (for Apache Tomcat would be $ TOMCAT_HOME / webapps) .
2. Once they are found, there are two possible ways: servlets are loaded immediately or only when someone wants to use them. By using term “load servlet” I mean the execution of its constructor and init () method. init () is executed only once in the life cycle of servlet . It allows you to prepare it for usage (eg to connect to the database, initialize any resources).
Code below presents fragment of web.xml file allowing to define order in which servlets are loaded when the containter starts:
<web-app>
...
<servlet>
<servlet-name>FirstToLoadServlet</servlet-name>
<servlet-class>pl.tdziurko.scwcd.servlets.FirstToLoadServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>SecondToLoadServlet</servlet-name>
<servlet-class>pl.tdziurko.scwcd.servlets.SecondToLoadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
</web-app>

