[Listener] Listener Basics

1. Listener

One of the three major components of Java Web (servlet, filter and listener); Listener > Filter > Servlet

A listener is an object that is specially used to monitor and process events or state changes on other objects. When something happens to the monitored object, it will take corresponding actions immediately. A listener is actually an ordinary Java class that implements a specific interface. This program is specially used to listen for method calls or property changes of another Java object. When the above events occur on the monitored object, a method of the listener will be executed immediately.

2. Listener in Java Web

Listener in JavaWeb is a * * * special class * * * defined in Servlet specification. It is used to listen to the creation and destruction events of domain objects such as ServletContext, HttpSession and ServletRequest in web applications, as well as the events of property modification in these domain objects.

3. Classification of Servlet listeners

Various types of listeners are defined in the Servlet specification. The event sources used for listening are ServletContext, HttpSession and ServletRequest. The Servlet specification divides various types of listeners into three types for the operations on these three objects:

  • An event listener that listens to the creation and destruction of the domain object itself;
  • An event listener that listens to the addition and deletion of attributes in the domain object;
  • An event listener that listens to the state of an object bound to the HttpSession domain.

4. Listen for the creation and destruction of ServletContext domain objects

The ServletContextListener interface is used to listen for creation and destruction events of ServletContext objects. Classes that implement the ServletContextListener interface can listen to the creation and destruction of ServletContext objects.

  • When the ServletContext object is created, the contextInitialized (ServletContextEvent sce) method is fired;
  • When the ServletContext object is destroyed, the contextDestroyed(ServletContextEvent sce) method is fired.

ServletContext domain object creation and destruction time:

  • Create: the server starts to create a ServletContext for each Web application;
  • Destroy: close the ServletContext representing each web application before closing the server;
  • "Live with heaven and earth".

5. Test

5.1. Write a listener for ServletContext

package com.qfedu.user.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

* @ClassName: MyServletContextListener
* @Description: MyServletContextListener Class implements the ServletContextListener interface,
*                 Therefore, you can listen to the creation and destruction of ServletContext objects.
public class MyServletContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("ServletContext objects creating");

    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ServletContext Object destruction");

5.2. Configure the listener of ServletContext

We register the listener in the web.xml file

<!-- Register for ServletContext Object to listen on -->
    <description>ServletContextListener monitor</description>
    <!--Realized ServletContextListener Listener class for interface -->

5.3 test

@WebServlet(name = "ServletListener", value = "/ServletListener")
public class ServletListener extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("visit ServletListener Interface");
        ServletContext servletContext = request.getServletContext();

Start project, access http://localhost:8080/javaweb__ 01_ war_ Expanded / ServletListener, print the ServletContext object creation first, and then print the access ServletListener interface

The same is true for other listeners

Tags: Java JavaEE server

Posted on Sat, 30 Oct 2021 01:24:29 -0400 by mananx