aspose file tools*
The moose likes Servlets and the fly likes Multiple ServletContextListner event handling order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple ServletContextListner event handling order" Watch "Multiple ServletContextListner event handling order" New topic
Author

Multiple ServletContextListner event handling order

Lance Miller
Greenhorn

Joined: Jun 23, 2008
Posts: 21
I've tried to find this information in the Servlet specification but am not able to. Is the order of the handling of events by multiple configured ServletContextListeners guaranteed? In Tomcat 6.0.26 I've noticed the it seems that initialized events are handled in the same order as the listing of the listeners in the web.xml file. And the destroy events are handled in the reverse order. For example, given the following:



Based on logging, the sequence of events is:


Is this guaranteed by the spec? Or just the way Tomcat is implemented?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is a link to the spec in my signature.
For a spec, it's a pretty readable document.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Lance Miller
Greenhorn

Joined: Jun 23, 2008
Posts: 21
Thanks Ben. When I looked in the spec yesterday before posting, I could not find the information. I did find it today. A fresh set of eyes I guess. Anyway, for anyone that finds this thread in the future or is curious, here is the info. The container is suppose to call the listeners in the order they are added during startup, and reverse order during shutdown.

11.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.
11.3.4 Notifications At Shutdown
On application shutdown, listeners are notified in reverse order to their declarations with notifications to session listeners preceding notifications to context listeners. Session listeners must be notified of session invalidations prior to context listeners being notified of application shutdown.

(Empahsis Added)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Great.
Thanks for posting back.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple ServletContextListner event handling order