servlet listener

Note: The loading order of web.xml is: [Context-Param]-> [Listener] ->[Filter]->[Servlet], and the order of actual program calls between the same type is based on the corresponding Mapping order of calls.

 

Servlet Listener interface and event (Event) object

 

The Servlet API provides different types of listeners for different types of events. The listener interface declares methods to handle a similar set of events, for example we have a ServletContext Listener listening for context startup and shutdown events. Every method in the listener interface takes an event object as input. The event object acts as a wrapper, providing listeners with specific objects.

 

The Servlet API provides the following event objects:

 

  1. javax.servlet.AsyncEvent - Event fired when an asynchronous operation started by a ServletRequest (by calling ServletRequest#startAsync or ServletRequest#startAsync(ServletRequest,ServletResponse) ) has completed, timed out, or produced an error.
  2. javax.servlet.http.HttpSessionBindingEvent - sends an event of this type to an object implementing HttpSessionBindingListener when that object is bound or unbound from a session, or to a HttpSessionAttributeListener configured in web.xml when any attribute is bound , unbind or replace in the session. Sessions bind objects by calling HttpSession.setAttribute and unbind objects by calling HttpSession.removeAttribute. We can use this event for cleanup activities when an object is removed from the session.
  3. javax.servlet.http.HttpSessionEvent − This is the class representing event notifications of session changes in web applications.
  4. javax.servlet.ServletContextAttributeEvent - Event class for notifications about changes to attributes of a web application's ServletContext.
  5. javax.servlet.ServletContextEvent - This is the event class for notifications about changes to the web application's servlet context.
  6. javax.servlet.ServletRequestEvent - Such events represent lifecycle events of ServletRequest. The source of the event is the ServletContext of this web application.
  7. javax.servlet.ServletRequestAttributeEvent − This is the event class used for notification of changes to attributes requested by servlets in the application.

 

The Servlet API provides the following listener interfaces:

 

  1. javax.servlet.AsyncListener - The listener will be notified if an asynchronous operation started on a ServletRequest to which the listener has been added has completed, timed out, or caused an error.
  2. javax.servlet.ServletContextListener - Interface for receiving notification events about ServletContext lifecycle changes.
  3. javax.servlet.ServletContextAttributeListener - Interface to receive notification events about ServletContext attribute changes.
  4. javax.servlet.ServletRequestListener - Interface for receiving notification events about requests coming in and out of the scope of a web application.
  5. javax.servlet.ServletRequestAttributeListener - Interface to receive notification events about ServletRequest attribute changes.
  6. javax.servlet.http.HttpSessionListener - Interface to receive notification events about HttpSession lifecycle changes.
  7. javax.servlet.http.HttpSessionBindingListener - Causes objects to be notified when they are bound from session to binding or from it.
  8. javax.servlet.http.HttpSessionAttributeListener - Interface for receiving notification events about HttpSession attribute changes.
  9. javax.servlet.http.HttpSessionActivationListener - Objects bound to a session may listen to container events notifying them that the session will be passivated and that the session will be activated. Containers that migrate sessions between VMs or persistent sessions are required to notify all properties bound to sessions that implement HttpSessionActivationListener.

 

Servlet's Listener can be configured in two ways

 

      1. We can use the @WebListener annotation to declare a class as a Listener, but the class should implement one or more Listener interfaces.

 1 package com.journaldev.listener;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.ServletContextEvent;
 5 import javax.servlet.ServletContextListener;
 6 import javax.servlet.annotation.WebListener;
 7 
 8 import com.journaldev.db.DBConnectionManager;
 9 
10 @WebListener
11 public class AppContextListener implements ServletContextListener {
12 
13     public void contextInitialized(ServletContextEvent servletContextEvent) {
14         ServletContext ctx = servletContextEvent.getServletContext();
15         
16         String url = ctx.getInitParameter("DBURL");
17         String u = ctx.getInitParameter("DBUSER");
18         String p = ctx.getInitParameter("DBPWD");
19         
20         //create database connection from init parameters and set it to context
21         DBConnectionManager dbManager = new DBConnectionManager(url, u, p);
22         ctx.setAttribute("DBManager", dbManager);
23         System.out.println("Database connection initialized for Application.");
24     }
25 
26     public void contextDestroyed(ServletContextEvent servletContextEvent) {
27         ServletContext ctx = servletContextEvent.getServletContext();
28         DBConnectionManager dbManager = (DBConnectionManager) ctx.getAttribute("DBManager");
29         dbManager.closeConnection();
30         System.out.println("Database connection closed for Application.");
31         
32     }
33     
34 }

 

      2. We can define the listener in web.xml:

<listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325139012&siteId=291194637