J2EE搭建之十一 如何设置start-on-load

What is load-on-startup servlet element in web.xml with Example?
Read more:
http://javarevisited.blogspot.com/2011/12/load-on-startup-servlet-webxml-example.html#ixzz2HGk8iDIn

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 example As 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.
 

你可能感兴趣的:(start-on-load)