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

I donโ€™t understand what the student classes do. For example, in this project there is a listener class referenced like this:

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

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

+8
source share
4 answers

I would suggest looking at the chapter "Application Life Cycle Events" from the Servlet specification.

Depending on which version you are using, here are the relevant chapters and links to documents:

Listeners are used to notify about events in web applications, including state changes of ServletContext , HttpSession and ServletRequest . By implementing the predefined listener interfaces ( javax.servlet.ServletContextListener , javax.servlet.http.HttpSessionListener , javax.servlet.ServletRequestListener , etc.), the servlet container will notify you of some events that occur in your application. They have many potential features, such as performing one-time installation and shutdown tasks for applications, intercepting requests for logging, tracking HTTP session usage, etc.

+4
source

The listener class receives notifications of selected events, such as launching an application or creating a new session.

Listener classes:

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

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

If you want your class to listen to application startup and shutdown, events implement the ServletContextListener interface. If you want your class to listen to session and revocation events, then implement the HttpSessionListener interface.

A source

+6
source

Yes, it is they who listen to some todo action, for example, if its contextloaderlistener then listens for the context load event, and there are many things that we can do at such an event so that they are done for this

+2
source

In general, the listener is the observer / subscriber side in the observer pattern. The server / framework side provides you with a means to notify of an event and, therefore, gives you the ability to do your actions.

And it does not have to be a "big project." Listeners are comfortable even in small ones :).

+2
source

All Articles