Servlet listeners : ServletRequestListener

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In the previous part of this series we learned about HttpSessionListener which is used to keep track of HttpSession lifecycle events i.e. session creation and session destruction. This part will cover ServletRequestListener which is used to listen to lifecycle events for a ServletRequest .

What is the need?

We may like to receive a notification whenever a request for a resource is made from the client so that we can log it.

How can we achieve it?

Java EE specification provides an interface called ServletRequestListener which receives notifications whenever a new request is about to come to the web application.  This is what javadoc says about it.

A ServletRequest is defined as coming into scope of a web application when it is about to enter the first servlet or filter of the web application, and as going out of scope as it exits the last servlet or the first filter in the chain.

requestInitialized(ServletRequestEvent event) :

Receives notification that a ServletRequest is about to come into scope of the web application.

requestDestroyed(ServletRequestEvent event) :

Receives notification that a ServletRequest is about to go out of scope of the web application.

Step 1: Create a dynamic web project

Create a dynamic web project as per this tutorial.

Step 2: Create a HelloServlet to serve request.

Here, we will create a servlet called HelloServlet which will simply receive some request from a web browser and do nothing. It is as follows.

Configure HelloServlet in web.xml

Step 3: Create a ServletRequestListener.

Now we will create a ServletRequestListener which receives the notifications whenever some request is being sent for any resource in this web application.

Configure this listener in  web.xml  as follows.

 Step 4: Run application.

Now we are ready to go. Deploy application under tomcat and start the server. Hit the url http://localhost:8080/ServletListenerExamples/HelloServlet . You will see this output on the console.

 

 

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

5 thoughts on “Servlet listeners : ServletRequestListener

  • Pingback:servlet listeners: ServletRequestAttributeListener | theJavaGeek

  • January 11, 2015 at 7:27 pm
    Permalink

    why a servletrequestlistener is not bound to a servlet, but to the whole application ?

    Reply
  • July 9, 2015 at 1:05 pm
    Permalink

    Hi, How can we map a request listener to a particular servlet or request. Is it possible.?

    Reply
  • April 14, 2016 at 6:22 pm
    Permalink

    To find which request is being processed

    public void requestDestroyed(ServletRequestEvent sre) {
    HttpServletRequest req = (HttpServletRequest)sre.getServletRequest();
    System.out.println(“Requested Completed for ” + req.getRequestURI());
    }

    Reply
  • September 27, 2017 at 11:10 am
    Permalink

    public void requestDestroyed(ServletRequestEvent event) {
    System.out.println(“request being sent to ”
    + event.getServletRequest().getRemoteAddr());
    }

    Shouldn’t this have been : “response being sent to ….”?
    The ‘request’ is not being sent anywhere. At this point the request has been processed and the RESPONSE is about to be sent.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *