3.6 Load on Startup

Java Servlet Programming, 2nd Edition > 3. The Servlet Lifecycle > 3.6 Load on Startup

 
< BACKCONTINUE >

3.6 Load on Startup

To have the PrimeSearcher start searching for primes as quickly as possible, we can configure the servlet's web application to load the servlet at server start. This is accomplished by adding the <load-on-startup> tag to the <servlet> entry of the deployment descriptor, as shown in Example 3-8.

Example 3-8. Loading a Servlet on Startup
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"     "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app>     <servlet>         <servlet-name>             ps         </servlet-name>         <servlet-class>             PrimeSearcher         </servlet-class>         <load-on-startup/>     </servlet> </web-app>

This tells the server to create an instance of PrimeSearcher under the registered name ps and init( ) the servlet during the server's startup sequence. The servlet can then be accessed at the URL /servlet/ps. Note that the servlet instance handling the URL /servlet/PrimeSearcher is not loaded at startup.

The <load-on-startup> tag shown in Example 3-8 is empty. The tag can also contain a positive integer indicating the order in which the servlet should be loaded relative to other servlets in the context. Servlets with lower numbers are loaded before those with higher numbers. Servlets with negative values or noninteger values may be loaded at any time in the startup sequence, with the exact order depending on the server. For example, the web.xml shown in Example 3-9 guarantees first is loaded before second, while anytime could be loaded anytime during the server startup.

Example 3-9. A Little Servlet Parade
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"     "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app>     <servlet>         <servlet-name>             first         </servlet-name>         <servlet-class>             First         </servlet-class>         <load-on-startup>10</load-on-startup>     </servlet>     <servlet>         <servlet-name>             second         </servlet-name>         <servlet-class>             Second         </servlet-class>         <load-on-startup>20</load-on-startup>     </servlet>     <servlet>         <servlet-name>             anytime         </servlet-name>         <servlet-class>             Anytime         </servlet-class>         <load-on-startup/>     </servlet> </web-app>


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net