Java-listener, java listener example
A Listener is also called a Listener. It is a Servlet Listener that can listen to client requests and server operations. Through the listener, some operations can be automatically triggered, such as the number of online users.
For example, ServletContextListener listens to the ServletContext object.
Create a web Project
Create CountUtils and ListenerTest classes and write code
Configure web. xml
Compile a jsp page
Run the code and access it (two people access it at the same time)
Common listening interfaces:
ServletContextAttributeListener listens for operations on the attributes of ServletContext, such as adding, deleting, and modifying attributes.
ServletContextListener listens to the ServletContext object.
When a ServletContext is created, the contextInitialized (ServletContextEvent sce) method is activated;
When ServletContext is destroyed, the contextDestroyed (ServletContextEvent sce) method is triggered.
HttpSessionListener:
When a Session is created, the sessionCreated (HttpSessionEvent se) method is activated;
When a Session is destroyed, the sessionDestroyed (HttpSessionEvent se) method is triggered.
HttpSessionAttributeListener:
When an attribute is added to a Session, the attributeAdded (HttpSessionBindingEvent se) method is triggered;
When a Session deletes an attribute, the attributeRemoved (HttpSessionBindingEvent se) method is triggered;
When the Session attribute is reset, The attributeReplaced (HttpSessionBindingEvent se) method is triggered.