This week's giveaway is in the Java/Jakarta EE forum. We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line! See <servlet> <servlet-name>init</servlet-name> <servlet-class>com.msdw.aps.taxportal.util.common.TPInitServlet</servlet-class> <init-param> <param-name>CONFIG_MODE</param-name> <param-value>INTEG</param-value> </init-param> <init-param> <param-name>INTEG_DSN_FILE_TAXPORTAL</param-name> <param-value>/ms/user/d/deshjay/aps/taxportal/0.1/src/WebRoot/xml/xyz.dev.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
The nice thing about them is that the contextInitialized method gets called once and only once when the app loads. Also, no servlets are loaded until the contextInitialized method is done.
A much better way to inialize an app than by using servlets.
According to the Servlet spec, there is no guarantee that a servlet will stay loaded or only be loaded once during the lifetime of the app. (There can only be one instance but that instance can be loaded and unloaded multiple times)
SRV.2.3.4 End of Service The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between. When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down. Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit.
This makes using a servlet (with load-on-startup) a bit risky for application initialization.
ContextListeners let servlets do what they were intended to do; listen for and respond to requests. [ September 20, 2006: Message edited by: Ben Souther ]