You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Santosh Asbe <sa...@gmail.com> on 2005/10/18 06:15:17 UTC
classloader during tomcat startup
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
Re: classloader during tomcat startup
Posted by David Delbecq <de...@oma.be>.
Robert Graf-Waczenski a écrit :
>>SRV.14.2.12 ServletContextListener
>> public interface ServletContextListener extends
>> java.util.EventListener
>> All Superinterfaces: java.util.EventListener
>> Implementations of this interface receive notifications about changes
>>to the serv-
>> let context of the web application they are part of. To receive
>>notification events,
>> the implementation class must be configured in the deployment
>>descriptor for the
>> web application.
>> Since: v 2.3
>> See Also: ServletContextEvent
>>
>>
>
>Correct. My mistake. And my own listener classes are indeed
>registered in my web.xml(s) since the beginning, forgot this ;-)
>So my point of not having to configure it in web.xml is moot.
>
>
>
>>Last but not least, using a Servlet has the additional
>>advantage it can
>>be easily configured
>>if needed without ressorting to an additionnal config file in
>>the classpath.
>>
>>
>
>I don't see what you are trying to say here. What additional
>config file in the classpath are you talking about? (I'm not
>trying to offend you, i'm only asking for clarification.)
>
>
>
Just pointing out if he needs some configuration parameters for his static
block, he can use the <param> sections of the servlet config.
If you use the listener class and need some configuration, you need
to somehow read it from a file as there is not equivalent
section.
>My main problem with the servlet approach is that a servlet
>has a doGet() method and is therefore supposed to serve
>web requests, which is not needed in an init class.
>(Agreed, this is not a major problem, but i like to keep
>things as small and simple as possible, and a class that
>*can* serve web requests should also actually be used for
>this purpose.)
>
>
Agree on that point, though i most of the time simply don't care :)
>Robert
>
>
>
>>Robert Graf-Waczenski a écrit :
>>
>>
>>
>>>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.
>>>
>>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: classloader during tomcat startup
Posted by Robert Graf-Waczenski <rg...@lsoft.com>.
> SRV.14.2.12 ServletContextListener
> public interface ServletContextListener extends
> java.util.EventListener
> All Superinterfaces: java.util.EventListener
> Implementations of this interface receive notifications about changes
> to the serv-
> let context of the web application they are part of. To receive
> notification events,
> the implementation class must be configured in the deployment
> descriptor for the
> web application.
> Since: v 2.3
> See Also: ServletContextEvent
Correct. My mistake. And my own listener classes are indeed
registered in my web.xml(s) since the beginning, forgot this ;-)
So my point of not having to configure it in web.xml is moot.
> Last but not least, using a Servlet has the additional
> advantage it can
> be easily configured
> if needed without ressorting to an additionnal config file in
> the classpath.
I don't see what you are trying to say here. What additional
config file in the classpath are you talking about? (I'm not
trying to offend you, i'm only asking for clarification.)
My main problem with the servlet approach is that a servlet
has a doGet() method and is therefore supposed to serve
web requests, which is not needed in an init class.
(Agreed, this is not a major problem, but i like to keep
things as small and simple as possible, and a class that
*can* serve web requests should also actually be used for
this purpose.)
Robert
> Robert Graf-Waczenski a écrit :
>
> >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.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: classloader during tomcat startup
Posted by David Delbecq <de...@oma.be>.
Well, i saw the ServletContextListener thingies.
Here is what i read about it, not quite the same, in servlet 2.4
specifications:
SRV.10.3.2 Deployment Declarations
Listener classes are declared in the Web application deployment
descriptor using the
listener element. They are listed by class name in the order in which
they are to be
invoked.
SRV.10.3.3 Listener Registration
The Web container creates an instance of each listener class and
registers it for event
notifications prior to the processing of the first request by the
application. The Web
container registers the listener instances according to the interfaces
they implement
and the order in which they appear in the deployment descriptor. During Web
application execution, listeners are invoked in the order of their
registration.
It is also stated there:
SRV.14.2.12 ServletContextListener
public interface ServletContextListener extends
java.util.EventListener
All Superinterfaces: java.util.EventListener
Implementations of this interface receive notifications about changes
to the serv-
let context of the web application they are part of. To receive
notification events,
the implementation class must be configured in the deployment
descriptor for the
web application.
Since: v 2.3
See Also: ServletContextEvent
So, i agree you can create a ServletContextListener, but you must anyway
declare them in the web.xml. The container is not supposed to scan the .jar
and WEB-INF/classes for them, mainly because to know if a Class implement an
interface, you need to get the Class objet and so you run the static
code of the class.
Last but not least, using a Servlet has the additional advantage it can
be easily configured
if needed without ressorting to an additionnal config file in the classpath.
Robert Graf-Waczenski a écrit :
>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:delbd@oma.be]
>>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 <de...@oma.be> 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: users-unsubscribe@tomcat.apache.org
>>>>For additional commands, e-mail: users-help@tomcat.apache.org
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: classloader during tomcat startup
Posted by Robert Graf-Waczenski <rg...@lsoft.com>.
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:delbd@oma.be]
> 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 <de...@oma.be> 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: users-unsubscribe@tomcat.apache.org
> >>For additional commands, e-mail: users-help@tomcat.apache.org
> >>
> >>
> >>
> >>
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: classloader during tomcat startup
Posted by David Delbecq <de...@oma.be>.
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 <de...@oma.be> 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: users-unsubscribe@tomcat.apache.org
>>For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>>
>>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: classloader during tomcat startup
Posted by Santosh Asbe <sa...@gmail.com>.
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 <de...@oma.be> 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: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
Re: classloader during tomcat startup
Posted by David Delbecq <de...@oma.be>.
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org