When I register the same class implementing ServletContextListener twice or more its' contextInitialized methods only gets called once. The servlet specifications (2.4 and 2.5) say the conforming container has to call the listeners in the same order they are registered in the deployment descriptor. The case that one listener class is registered twice is not mentioned explicitly in the specification, though. Example: <listener> <listener-class>test.ServletContextListener1</listener-class> </listener> <listener> <listener-class>test.ServletContextListener2</listener-class> </listener> <listener> <listener-class>test.ServletContextListener1</listener-class> </listener> test.ServletContextListener1 and test.ServletContextListener2 get called, but test.ServletContextListener1 does not get called again. Apache Tomcat 5.5.25 has the same bug.
This is explicitly not allowed as a fix for http://issues.apache.org/bugzilla/show_bug.cgi?id=3770 The change is here http://svn.apache.org/viewvc?view=rev&revision=287588 The problem is that it is hard to distinguish between when multiple listeners is a bug (ie 3770) and when it is intentional (this issue). There are a few places where the spec could be read as only expecting one instance of each listener class per webapp but it is far from clear. I am going to resolve this as WONTFIX since working around this issue is easier than trying working around 3770.
Thanks a lot for your fast reply. Yes I already recognized the spec is not that clear on this issue. Could you at least print a warning log message if a listener is registered more than once?
A warning seems reasonable. Re-opening and changing this to an enhancement. I've committed a fix to trunk and proposed it for 6.0.x. It probably won't make the 6.0.16 release but should make the following one.
This did make it into 6.0.16.
Thanks a lot Mark! This will definitely help the next one running into this.
When upgrading our developer boxes to tomcat 6.0.16, the following message is now appearing for many of the listeners that we use. Each of our webapps has one and only one listener defined of each class, but we are now getting the "The listener {0} is already configured for this context. The duplicate definition has been ignored." So, what's going on here? Should I just ignore these messages? Why are they showing up when it's clear that there are no duplicate definitions? Where else could these listener definitions be besides in the webapp's web.xml? Should I open a new ticket in Bugzilla? Or should this ticket be reopened?
This enhancement uncovered a bug where all listeners in web.xml were registered twice. See bug 44423.