You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by lizhg <li...@icss.com.cn> on 2007/04/24 07:33:52 UTC
Listener vs. load-on-startup
Hi all,I have some problem with Listener and load-on-startup,I serch on google and I find this article below,it do a lot good to me, but I still want to ask , using the init() of a servlet and the contextDestroyed() of a listener are equal to Iusing the contexInitialized() and the contextDestroyed() of a
listener?
On 9 Nov 2001, Dr. Evil wrote:
> Date: 9 Nov 2001 07:43:17 -0000
> From: Dr. Evil <dr...@sidereal.kz>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: API 2.3: Listener vs. load-on-startup
>
>
> I have a few classes that need to be loaded by Tomcat before it starts
> to serve any requests. One of these opens a database pool, another
> opens a logger, etc. The traditional way to do this is with
> load-on-startup like this:
>
> <servlet>
> <servlet-name>startlogging</servlet-name>
> <servlet-class>startlogging</servlet-class>
> <load-on-startup>1</load-on-startup>
> </servlet>
>
> which calls the init() method of the class.
>
> However, I need to have an object installed into the ServletContext
> object before any requests can be serviced. The init() method of a
> servlet seems to have no access to the ServletContext object (is this
> correct?).
You have access to the servlet context via the getServletContext() method
of the servlet -- it is pre-initialized to work for you. However, using a
listener is the better way to do this in a Servlet 2.3 environment.
> Because of this, I am thinking of using the new <listener>
> declaration to achieve this same goal. I can declare a listener like
> this:
>
> <listener>
> <listener-class>dostartupstuff</listener-class>
> </listener>
>
> and then in the dostartupstuff class, I declare a method:
>
> public void contexInitialized(ServletContextEvent e) { ... }
>
> which will be called as soon as the context is created.
>
> Is this the right way to do this?
Yes. That is exactly what context listeners are designed to do.
Why is it better than a servlet init() method? Because the container
gives you *no* guarantee that it will keep a servlet loaded for the
lifetime of the application (although many of them do), so if you clean up
your resources in the destroy() method -- such as closing database
connections -- this might happen to you at a bad time. The
contextDestroyed() method of your listener will not get called until the
application is really being shut down.
> And is there a way I can control
> the order in which listeners are called?
>
At startup time, listeners are called in the order they are defined in the
deployment descriptor -- at shutdown time, they are called in reverse
order. For more info, see the 2.3 spec:
http://java.sun.com/products/servlet/download.html
> Thanks
>
Craig
Re: Listener vs. load-on-startup
Posted by Jon Wingfield <jo...@mkodo.com>.
The order of events is:
Startup:
All listeners get called.
All filters, load-on-startup servlets get inited.
Shutdown:
All filters, servlets get destroyed.
All listeners get called.
So, it is possible for the combination of load-on-startup init +
contextDestroyed to do the same job as just using one
ServletContextListener. But why do it? IMO load-on-startup was a hack in
the early spec versions so that application data could be set up.
ServletContextListener was added as the way forward ;)
Regards,
Jon
lizhg wrote:
> Hi all,I have some problem with Listener and load-on-startup,I serch on google and I find this article below,it do a lot good to me, but I still want to ask , using the init() of a servlet and the contextDestroyed() of a listener are equal to Iusing the contexInitialized() and the contextDestroyed() of a
> listener?
>
> On 9 Nov 2001, Dr. Evil wrote:
>
>> Date: 9 Nov 2001 07:43:17 -0000
>> From: Dr. Evil <dr...@sidereal.kz>
>> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
>> To: tomcat-user@jakarta.apache.org
>> Subject: API 2.3: Listener vs. load-on-startup
>>
>>
>> I have a few classes that need to be loaded by Tomcat before it starts
>> to serve any requests. One of these opens a database pool, another
>> opens a logger, etc. The traditional way to do this is with
>> load-on-startup like this:
>>
>> <servlet>
>> <servlet-name>startlogging</servlet-name>
>> <servlet-class>startlogging</servlet-class>
>> <load-on-startup>1</load-on-startup>
>> </servlet>
>>
>> which calls the init() method of the class.
>>
>> However, I need to have an object installed into the ServletContext
>> object before any requests can be serviced. The init() method of a
>> servlet seems to have no access to the ServletContext object (is this
>> correct?).
>
> You have access to the servlet context via the getServletContext() method
> of the servlet -- it is pre-initialized to work for you. However, using a
> listener is the better way to do this in a Servlet 2.3 environment.
>
>> Because of this, I am thinking of using the new <listener>
>> declaration to achieve this same goal. I can declare a listener like
>> this:
>>
>> <listener>
>> <listener-class>dostartupstuff</listener-class>
>> </listener>
>>
>> and then in the dostartupstuff class, I declare a method:
>>
>> public void contexInitialized(ServletContextEvent e) { ... }
>>
>> which will be called as soon as the context is created.
>>
>> Is this the right way to do this?
>
> Yes. That is exactly what context listeners are designed to do.
>
> Why is it better than a servlet init() method? Because the container
> gives you *no* guarantee that it will keep a servlet loaded for the
> lifetime of the application (although many of them do), so if you clean up
> your resources in the destroy() method -- such as closing database
> connections -- this might happen to you at a bad time. The
> contextDestroyed() method of your listener will not get called until the
> application is really being shut down.
>
>> And is there a way I can control
>> the order in which listeners are called?
>>
>
> At startup time, listeners are called in the order they are defined in the
> deployment descriptor -- at shutdown time, they are called in reverse
> order. For more info, see the 2.3 spec:
>
> http://java.sun.com/products/servlet/download.html
>
>> Thanks
>>
>
> Craig
>
>
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Listener vs. load-on-startup
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Lizhg,
lizhg wrote:
> Hi all, I have some problem with Listener and load-on-startup, I search
> on google and I find this article below, it do a lot good to me, but I
> still want to ask , using the init() [and destroy()] of a servlet
> [is the same as] using the
> contexInitialized() and the contextDestroyed() of a listener?
It is generally considered good form to set up application-wide
configuration, objects, etc. in a Listener. For this type of use, you
want to use a ServletContextListener specifically (there are other types
of listeners).
If you have a servlet that needs to configure /itself/, then it's
perfectly reasonable to use the servlet's init() method for that purpose.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGLfI09CaO5/Lv0PARAqjuAJ0WX1EaGVf6mun6az6cR6/caXe0twCgqCgh
V+Z8d9ig1R2HHbrvy7KfrNU=
=zeEl
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org