Reputation: 900
I'm trying to implements initialization and shutdown of a webapp. That includes initialization and shutdown of:
Using Tomcat 5.5.30 and Java 6. My idea is to avoid resource leaking, mostly because of the redeploy of the webapp in the development environment.
How should I implement this?
Upvotes: 14
Views: 9350
Reputation: 189
Its also possible to use the HTTP Servlet instead but the listener is a better option.
You have to extend a class with HttpServlet and setting the following stuff to your web.xml:
<servlet>
<servlet-name>StartupServlet</servlet-name>
<servlet-class>your.package.servlets.StartupServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
The class can overwrite the init and the destroy method.
Upvotes: 1
Reputation: 1475
But still you want to manage your resources in such a way that they do not leak if the application crashes and normal shutdown routines are not called.
Upvotes: 0
Reputation: 181290
Usually for Web initialization and shutdown, you will write a ServletContextListener.
The steps to do this are:
javax.Servlet.ServletContextListener
web.xml
deployment descriptor to register the class you've just createdWhen you deploy your application, contextInitialized
method will be called. You can place all initialization you want here. On application shutdown contextDestroyed
method will be called.
Upvotes: 21