Using the load-on-startup mechanism was the way to go with older
servlet APIs. Nowadays, with servlet API 2.4, having a class
implement the ServletContextListener interface is better in my eyes.

Here you go:

public class MyInitClass implements ServletContextListener
{
        static {
                // do your static stuff during class loading
        }

        public void contextInitialized(ServletContextEvent event)
        {
                // do your init stuff
        }

        public void contextDestroyed(ServletContextEvent event)
        {
                // do your destroy stuff
                // e.g. cleanup static buffers etc.
        }
}

Tomcat automatically finds implementors of the ServletContextListener
interface and calls their interface methods to notify them of
their own context startup and their own context shutdown.
So if you have several contexts and for each of them several such
listener classes, tomcat calls the listener methods of the
correct (i.e. those belonging to the context) classes when the
context is started or shut down.

Your web.xml can remain unchanged for this.

Robert

> -----Original Message-----
> From: David Delbecq [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, October 19, 2005 9:51 AM
> To: Tomcat Users List
> Subject: Re: classloader during tomcat startup
>
>
> Creating an initialisation servlet for your webapp would be a
> good way.
> Create a servlet and map it in web.xml adding a
> <load-on-startup> element
> with a non zero value. In the servlet init you can then setup all
> classes you need.
>
> For classloading hierarchy in tomcat, take a look at
> http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html
>
> Santosh Asbe a écrit :
>
> >Hi,
> >  further to our discussion, can i specifically load a class
> during startup?
> >Is i possible in tomcat?
> > Where is the clasloader hierachy and details placed??
> > Santosh
> >
> >
> > On 10/18/05, David Delbecq <[EMAIL PROTECTED]> wrote:
> >
> >
> >>I don't know from the details of tomcat implementation, but
> i'll response
> >>using common sense and from my experience.
> >>
> >>Tomcat loads all .jar descriptor at webapp startup so it
> know which .jar
> >>contains
> >>which class. But it does not load the .class binary content;
> >>Each time a class is required, a Class is request to
> classloader by jvm.
> >>If this is first time classloader has to return the Class,
> it initialize
> >>it.
> >>
> >>So static block is run at first time class is requested.
> >>
> >>This is common sense as it prevents initialising classes
> you never use
> >>in your library.
> >>
> >>David Delbecq
> >>
> >>Santosh Asbe a écrit :
> >>
> >>
> >>
> >>>Hi all,
> >>>I have couple of questions regarding tomcat startup
> >>>1)when the tomcat is started , does it load all the jar
> file from its lib
> >>>and create a object of all the classes?
> >>>2) if there is a static block in one of the classes , when will it
> >>>execute..during startup or when first call is made
> >>>Santosh
> >>>
> >>>
> >>>
> >>>
> >>>
> >>------------------------------------------------------------
> ---------
> >>To unsubscribe, e-mail: [EMAIL PROTECTED]
> >>For additional commands, e-mail: [EMAIL PROTECTED]
> >>
> >>
> >>
> >>
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to