What is the purpose of a listener class in a large project

Caffeinated picture Caffeinated · Sep 22, 2011 · Viewed 22.8k times · Source

I'm confused about what listener classes do. For example, in this project there is a listener class referenced as so:

<listener>
    <listener-class>com.sun.javaee.blueprints.petstore.model.CatalogFacade</listener-class> 
</listener>

Is it as the name implies, just listening for actions to do?

Answer

P&#229;l Brattberg picture Pål Brattberg · Sep 22, 2011

Listener Classes get notified on selected events, such as starting up the application or creating a new Session.

Listener Classes :

These are simple Java classes which implement one of the two following interfaces :

  • javax.servlet.ServletContextListener
  • javax.servlet.http.HttpSessionListener

If you want your class to listen for application startup and shutdown events then implement ServletContextListener interface. If you want your class to listen for session creation and invalidation events then implement HttpSessionListener interface.

Source