load-on-startup is an chemical cistron which appears within <servlet> tag in web.xml.4 years dorsum load-on-startup was a real pop servlet interview question because non many Java J2EE developer was familiar amongst this chemical cistron as well as how load-on-startup industrial plant within servlet container similar tomcat or webshere. In this J2EE Tutorial nosotros volition run into what is charge on start up, how to purpose load-on-startup chemical cistron as well as what are dissimilar values nosotros tin configure for loadOnStartup within web.xml.
What is load-on-startup
database connections or performing ldap authentication which contribute network latency or whatever other fourth dimension consuming job, to avoid this, spider web container provides yous a hateful to specify for certain servlet to hold out loaded during deployment fourth dimension of application yesteryear using load-on-startup parameter.
If yous specify load-on-startup parameter within a servlet than based upon its value Container volition charge it.you tin specify whatever value to this chemical cistron simply inward illustration of load-on-startup>0 , servlet amongst less release volition hold out loaded first. For illustration inward below web.xml AuthenticationServlet volition hold out loaded earlier AuthorizationServlet because load-on-startup value for AuthenticationServlet is less (2) field for AuthorizationServlet is 4.
load-on-startup Example inward web.xml
hither is an illustration of how to purpose charge on startup tag within servlet chemical cistron inward 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 ii servlet than they volition hold out loaded inward an lodge on which they are declared within web.xml file.
2. if <load-on-startup> is 0 or negative integer than Servlet volition hold out loaded when Container feels to charge them.
3. <load-on-startup> guarantees loading, initialization as well as telephone band to init() method of servlet yesteryear spider web container.
4. If at that topographic point is no <load-on-startup> chemical cistron for whatever servlet than they volition hold out loaded when spider web container decides to charge them.
When to purpose <load-on-startup> inward web.xml
<load-on-startup> is suitable for those servlet which performs fourth dimension consuming jobs e.g. Creating Database Connection pool, downloading files or information from network or ready surroundings ready for servicing customer inward damage of initializing cache , clearing pipelines as well as loading of import information inward memory. If whatever of your servlet performs these jobs as well as therefore declare them using <load-on-startup> chemical cistron as well as specify lodge every bit per your draw of piece of work organisation logic or what suites your application. Remember lower the value of <load-on-startup>, servlet volition hold out loaded first. You tin besides cheque your spider web container documentation on how precisely charge on start-up is supported.
That’s all on charge on start-up tag of servlet chemical cistron inward web.xml. Use it carefully as well as it tin trim down response fourth dimension for your spider web application. You tin besides cheque my Struts interview questions as well as spring interview questions for to a greater extent than on J2EE interview.
Further Learning
Spring Framework 5: Beginner to Guru
Difference betwixt SendRedirect as well as Forward inward Servlet?
Difference betwixt HashMap as well as HashSet inward Java