How to ensure class initialization at container start up?
Hi guys...
I have a class that i need to initializate and instantiate before the server load the web application... in this case, the class is a connection pool to a legacy database (dbf database) so i need to ensure class initialization before beging to serving connections...
i found somewhere something called lifecycle listeners
[351 byte] By [
luis_rf40a] at [2007-10-2 13:09:24]

You could implement a ServletContextListener. It has two methods: contextInitialized and contextDestroyed. You could do any necessary setup work in contextInitialized and any cleanup work in contextDestroyed. You'll have to include a listener element in your web.xml for your ServletContextListener.
Thanks for the answer,.. i got a clue now...
I will create a servlet that will initialize and instantiate a pool class in the ServletContextInitialized event... this servlet will be market at fist to be loaded in web.xml....
you've awarded the duke's dollar...
I磛e other question: There's i way to avoid that the web application to be loaded by the container in case that database pool not able to be ready?
I want to show a "Service no available" page in case that there is a problem with database ? i was thinking in throwing an exception that would be caught in web app main controller but i think is it better to prevent container to load an application that will no work !!!!!
It's just a though...
Thanks in advance
I don't know how to prevent the application being loaded but there are other things you can do.
In the contextInitialized method of your ServletContextListener, if there is a problem initializing the database, set an attribute in the ServletContext. You can get the ServletContext via the getServletContext method of the ServletContextEvent you are passed in the contextInitialized method. The attribute can be anything you like, for example:
String OkToProceed = "N";
servletContextEvent.getServletContext().setAttribute("OkToProceed",OkToProceed);
Then have each of your servlets check for this attribute. You can do it in the init method of the servlet and have it throw an exception or you can do it in the doGet or doPost method and have it forward to a jsp which displays a "System not available" message.
If you have any jsps that are accessed directly (not through a servlet), the jsps will also have to check for this attribute in the servlet context (this is the application implicit object in jsps).
Thanks man...Your anwser was really helpfull...Now i got i clue were to start...
> Then have each of your servlets check for this
> attribute. You can do it in the init method of the
> servlet and have it throw an exception or you can do
> it in the doGet or doPost method and have it forward
> to a jsp which displays a "System not available"
> message.
>
> If you have any jsps that are accessed directly (not
> through a servlet), the jsps will also have to check
> for this attribute in the servlet context (this is
> the application implicit object in jsps).
In this case a better approach would be to use a filter. Instead of adding checks to each servlet - add it to one filter. If the attribute exists - end the filterchain in the filter.
Another approach would be to attempt to setup the resources after the initial attempt (if temp unavailable) at regular intervals (quartz) - every time someone makes a call to a unitialized pool - throw an exception and map it as an error-code in web.xml.
- J鴕gen L鴎ke -
A filter is definitely the better way to do it.Ignore what I said about having each servlet check for the attribute.