Pregunta

I am wondering how I can remove an EventListener from a ServletContext (http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html). The later offers a addListener method but nothing to remove a listener. Are listeners maybe hold by weak references and can be deregistered by loosing all strong references? Or what happens if my application is undeployed? Is the listener still active?

¿Fue útil?

Solución

This is not an answer to the question , I would just like to make few points .

The later offers a addListener method but nothing to remove a listener.

Because you need to make a decision initially whether to have a listener or not . Look at the Javadoc:

Interface for receiving notification events about ServletContext lifecycle changes.

In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with WebListener, or registered via one of the addListener methods defined on ServletContext.

The addListener() gives you a provision to register multiple types of listener , not just the ServletContextListener. It is just a method which could be used by you in case you choose not to declare the listeners up front in web.xml.

The given listener must be an instance of one or more of the following interfaces:

  1. ServletContextAttributeListener
  2. ServletRequestListener
  3. ServletRequestAttributeListener
  4. HttpSessionListener
  5. HttpSessionAttributeListener

what happens if my application is undeployed

All the listener objects will be destroyed along with the app. Listeners are instantiated and registered in the Web container at the time of the deployment of the Web application.

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. During Web application execution, listeners are invoked in the order of their registration. 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 invalidation prior to context listeners being notified of application shutdown.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top