Use Java EE deployment descriptors to define context lifecycle listeners

This rule flags all Context <Listener> elements in the META-INF/context.xml file.

In Apache Tomcat, Java objects that need to know when their context is started or stopped are declared by including a Listener element within the context. The class name specified in the Listener element implements the org.apache.catalina.LifecycleListener interface. That class will be notified about the occurrence of the corresponding lifecycle events. Note that the Do not use Apache Tomcat packages and APIs rule flags occurrences of the use of org.apache.catalina.LifecycleListener .

The analogous Java EE functionality is available using javax.servlet.ServletContextListener and ServletContextEvent and the corresponding Listener entries in the web.xml file.

Tomcat allows Context <Listener> elements to be defined in these locations:

The migration tool only flags Listener elements in the META-INF/context.xml file. Listener information defined in the other locations can be copied to the META-INF/context.xml file in order for it to be processed.

The recommended action is to convert the Java files using org.apache.catalina.LifecycleListener to use javax.servlet.ServletContextListener and to add the appropriate listener entries to the web.xml file for the application.

For additional information on the servlet lifecycle listeners:

For additional information on lifecycle listeners in Tomcat see: