Friday, December 16, 2011

What is load-on-startup servlet element in web.xml with Example?

load-on-startup is an element which appears inside <servlet> tag in web.xml.4 years back load-on-startup was a very popular servlet interview question because not many Java J2EE developer was familiar with this element and how load-on-startup works inside servlet container like tomcat or webshere. In this J2EE Tutorial we will see what is load on start up, how to use load-on-startup element and what are different values we can configure for loadOnStartup inside web.xml.

What is load-on-startup
load on startup servlet tag exampleAs stated earlier load-on-startup is a tag element which appear inside <servlet> tag in web.xml. load-on-startup tells the web container about loading of a particular servlet. if you don't specify load-on-startup then container will load a particular servlet when it feels necessary most likely when first request for that servlet will come, this may lead to longer response time for that query if Servlet is making database connections or performing ldap authentication which contribute network latency or any other time consuming job, to avoid this, web container provides you a mean to specify certain servlet to be loaded during deployment time of application by using load-on-startup parameter.

If you specify load-on-startup parameter inside a servlet than based upon its value Container will load it.you can specify any value to this element but in case of load-on-startup>0 , servlet with less number will be loaded first. For example in below web.xml AuthenticationServlet will be loaded before AuthorizationServlet because load-on-startup value for AuthenticationServlet is less (2) while for AuthorizationServlet is 4.

load-on-startup Example in web.xml

here is an example of how to use load on startup tag inside servlet element in web.xml:

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

Important points on load-on-startup element

1. If <load-on-startup> value is same for two servlet than they will be loaded in an order on which they are declared inside web.xml file.
2. if <load-on-startup> is 0 or negative integer than Servlet will be loaded when Container feels to load them.
3. <load-on-startup> guarantees loading, initialization and call to init() method of servlet by web container.
4. If there is no <load-on-startup> element for any servlet than they will be loaded when web container decides to load them.

When to use <load-on-startup> in web.xml
<load-on-startup> is suitable for those servlet which performs time consuming jobs e.g. Creating Database Connection pool, downloading files or data from network or prepare environment ready for servicing client in terms of initializing cache , clearing pipelines and loading important data in memory. If any of your servlet performs these jobs then declare them using <load-on-startup> element and specify order as per your business logic or what suites your application. Remember lower the value of <load-on-startup>, servlet will be loaded first. You can also check your web container documentation on how exactly load on start-up is supported.


That’s all on load on start-up tag of servlet element in web.xml. Use it carefully and it can reduce response time for your web application. You can also check my Struts interview questions and spring interview questions for more on J2EE interview.


Related Tutorials

4 comments :

VJ said...

very useful and detailed explanation.

Rakesh said...

A good example of load on startup is spring's dispatcher servlet which is always specified with load-on-startup tag value:
dispatcher org.springframework.web.servlet.DispatcherServlet 2 </servlet

Javin @ Servelt Interview Questions said...

It looks like blogger ate < tag , anyway I don't think you can put Servlet element inside another servlet element, you got to check web.xml DTD which says what can go inside servlet tag. but most probably answer is no.

Diyyana Kishore Babu said...

Diyyana Kishore Babu:-

Hi Javin, one small add on point to ‘’. When web container started and while running, it loads the deployed web application, during this web container creates the servlet context object based on the information provided in web.xml file. Once this provided for the particular servlet class it give the priority order to this sequence defined servlet classes to load first then subsequent servlet classes has to load. For instance this is useful during context loading if there are no of frameworks developed based on servelts (Struts/JSF/Spring-MVC etc..), which has to load first and next during their dependencies at runtime.

Post a Comment