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

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


I'd suggest reviewing the chapter on "Application Lifecycle Events" from the Servlet specification.

Depending on which version you're using, here are the corresponding chapters and links to the docs:

  • Servlet 3.0 : Chapter 11
  • Servlet 2.5 : Chapter 10
  • Servlet 2.4 : Chapter 10

Listeners are used to be notified of events to web applications, including state changes in the ServletContext, HttpSession, and ServletRequest objects. By implementing predefined listener interfaces (javax.servlet.ServletContextListener, javax.servlet.http.HttpSessionListener, javax.servlet.ServletRequestListener, etc.), the servlet container will notify you of certain events that are happening in your application. They have a lot of potential uses, such as performing one-time application setup and shutdown tasks, intercepting requests to perform logging, tracking HTTP session use, etc.


Yes exactly they are listening for some action todo, for example if its contextloaderlistener then it will listen to context loading event and there are many stuff that we can do at such event so these are made for that