Thursday, January 17, 2013

What is load-on-startup

load-on-startup is a tag element which appear inside 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.

Eg:
<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 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 is 0 or negative integer than Servlet will be loaded when Container feels to load them.
3. guarantees loading, initialization and call to init() method of servlet by web container.
4. If there is no element for any servlet than they will be loaded when web container decides to load them.

When to use in web.xml 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 element and specify order as per your business logic or what suites your application. Remember lower the value of , servlet will be loaded first.
Ref: javarevisited

No comments:

Post a Comment