Kamis, 20 Maret 2014

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




load-on-startup is an element which appears inside 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 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:








AuthenticationServlet


AuthenticationServlet


com.trading.AuthenticationServlet


2










AuthorizationServlet


AuthorizationServlet


com.trading.AuthorizationServlet


4







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






























Source:http://javarevisited.blogspot.com/2011/12/load-on-startup-servlet-webxml-example.html

Tidak ada komentar:

Posting Komentar