Servlet Listener

In the previous tutorial, we have learned about Servlet Filters, servlet in this tutorial; we will learn Servlet Listener.

Introduction of Servlet Listener:

Servlet Listener allow us to do event handling in the web container. On any event occurrence, we can perform a specific task like counting, finding current logged in user, Now, what is an event? An event is the change of state of an object. When an event is triggered, the corresponding listener will be executed.

List of Events:

  • Added
  • Removed
  • Replaced
  • Initialized
  • Disproved
  • Creation
  • Invalidation
  • Timeout
  • Activation
  • Passivization

List of Event Classes

  • ServletContextAttributEvent
  • HTTPSessionBindingEvent
  • ServlerRequestAttributeEvent
  • ServletContextEvent
  • HTTPSessionEvent
  • HTTPSessionBindingEvent
  • ServletRequestEvent

List of Servlet Listener Interfaces:

  • ServletContextAttributeListners
  • HTTPSessionAttributeListners
  • ServletRequestAttributeListners
  • ServletContextListners
  • HTTPSessionListners
  • HTTPSesssonActivationListners
  • HTTPSessionBindingListners
  • ServletRequestListners

How these Event Classes and Listener Interfaces Work together?

In Servlet Context object when the attribute is added, removed or replaced ServletContextAttribute will be generated this event is then used to listen ServletContextAttributeListners.

In Session Object when the attribute is added, removed or replaced HTTPSessionBindingEvent will be generated this event is then used to listen HTTPSessionAttributeListners.

In Request Object when the attribute is added, removed or replaced ServlerRequestAttributeEvent will be generated this event is then used to listen ServletRequestAttributeListners.

When Context object is Initialized or destroyed a ServletContextEvent will be generated this event is then used to listen ServletContextListners.

When Session object is created, invalidated or timeout  an HTTPSessionEvent will be generated this event is then used to listen HTTPSessionListners

When Session object is activated or passivated  an HTTPSessionEvent will be generated this event is then used to listen HTTPSessionActivationListners

When Request object is initialized or destroyed a ServletRequestEvent will be generated this event is then used to listen ServletRequestListner

 

Difference between Servlet listeners and Servlet filters

  • The filter is called on each URL request while the calling of listeners depends on the specific event occurrence.
  • Filters are the objects used to intercept, transform or manipulate incoming requests and outgoing responses from the servlet while the Listeners objects are used to monitor or react to an event.
  • Filters belong to javax.servlet.Filters while Listeners belongs to javax.servlet.Listener package

Filter snippet:

Servlet Listeners Snippet:

Below is the code snippet of one of the many  “ServletContextListeners.”

Creating Servlet listener:

    1. Open the navigation tab.
    2. Right click on the project that we have previously created
    3. Click on “New.”
    4. Select “Other”, this will open up a new Selection Wizard window
      Prepare for Creating Filter
      Prepare for Creating Filter

       

    5. Find “Web” Folder in it and expand it
    6. Search for Listeners under “The Web” and select it
    7. Click on “Next.”
      Selecting Servlet Listener
      Selecting Servlet Listener

       

    8. Clicking Next will open up another window click Browse to select an existing Package name
    9. Select your package in which you want to create your Listener.
    10. Click Ok.
    11. Write Your Listener Class Name as “MyFirstListner”
    12. Click “Next”
      Selecting Destination and Name for Servlet Listner
      Selecting Destination and Name for Servlet Listener

       

    13. Select Lifecycle under servlet context events.
    14. Click “Finish”.
      Seleting Servlet Listener Interface
      Selecting Servlet Listener Interface

       

Code:

Download Complete Servlets Project Here

Hope you like this tutorial. From the next tutorial, you will start learning about JSP. Stay tuned for more upcoming tutorials. Stay Blessed!