Well in this blog I am writing about an element load-on-startup of web.xml file.
Have you noticed <load-on-startup> tag that appears inside the <servlet> tag. <load-on-startup> tag tells the web container about the loading of a particular servlet.
For any request to be fulfilled by the servlet, the servlet container has to load the servlet. The loading of servlet can happen when the servlet engine itself is started, or later when a client request is actually delegated to the servlet. If we do not specify <load-on-startup> tag, then the servlet container will load the servlet when it feels necessary to load. Most likely is that it will load when the first client request comes to that particular servlet.
There is a drawback associated with this loading method. It can result in longer response time for that query if it is making a database connection or performing a ldap authentication.
This is where the <load-on-startup> tag becomes important. By using <load-on-startup> parameter we can ask the servlet container to load the servlet during deployment time of application to avoid the delay in response.
In short <load-on-startup> specifies that the servlet should be loaded automatically when the web application is started.
Also one important thing to know is that <load-on-startup> gurantees loading and initialization by calling init() of the servlet.
<load-on-startup> sub-element is also used to tell the container about the order in which each servlet should be loaded. Based on the value of <load-on-startup> the container will load the servlets.
Any value greater that 0 can be assigned to load on startup parameter. The servlet which has smaller value in <load-on-startup> will be loaded earlier compared to servlet with larger valuel <load-on-startup> servlet.
One more thing to know is that if two servlets have same value in for their <load-on-startup>, then they will be loaded in the order in which they are declared in web.xml file.
Again a question may arise that I have mentioned that positive values greater than zero should be assigned in <load-on-startup> tag but what if we give a 0 or a negative number?
In such a situation the servlet container will behave as if there is no <load-on-startup> tag mentioned for that servlet and hence servlet will be loaded when the servlet-container feels to load it.
For any request to be fulfilled by the servlet, the servlet container has to load the servlet. The loading of servlet can happen when the servlet engine itself is started, or later when a client request is actually delegated to the servlet. If we do not specify <load-on-startup> tag, then the servlet container will load the servlet when it feels necessary to load. Most likely is that it will load when the first client request comes to that particular servlet.
There is a drawback associated with this loading method. It can result in longer response time for that query if it is making a database connection or performing a ldap authentication.
This is where the <load-on-startup> tag becomes important. By using <load-on-startup> parameter we can ask the servlet container to load the servlet during deployment time of application to avoid the delay in response.
In short <load-on-startup> specifies that the servlet should be loaded automatically when the web application is started.
Also one important thing to know is that <load-on-startup> gurantees loading and initialization by calling init() of the servlet.
<servlet>
<servlet-name>TestServlet </servlet-name>
<servlet-classp>TestServlet </servlet-classp>
<load-on-startup>1 </load-on-startup>
</servlet>
<servlet-name>TestServlet </servlet-name>
<servlet-classp>TestServlet </servlet-classp>
<load-on-startup>1 </load-on-startup>
</servlet>
<load-on-startup> sub-element is also used to tell the container about the order in which each servlet should be loaded. Based on the value of <load-on-startup> the container will load the servlets.
Any value greater that 0 can be assigned to load on startup parameter. The servlet which has smaller value in <load-on-startup> will be loaded earlier compared to servlet with larger valuel <load-on-startup> servlet.
One more thing to know is that if two servlets have same value in for their <load-on-startup>, then they will be loaded in the order in which they are declared in web.xml file.
Again a question may arise that I have mentioned that positive values greater than zero should be assigned in <load-on-startup> tag but what if we give a 0 or a negative number?
In such a situation the servlet container will behave as if there is no <load-on-startup> tag mentioned for that servlet and hence servlet will be loaded when the servlet-container feels to load it.