javax.servlet.ServletRequestListener :-


ServletRequestListener interface gets notified each time request came in or going out of scope of application. You can use it to know each time request came in and goes out of scope.
It has the following methods -

1- requestInitialized() - This method invokes when a request is about to come to web-app. It has the following syntax -
 public void requestInitialized(ServletRequestEvent event)   

2- requestDestroyed() - This method invokes when a request is about to go out of scope of web-app. It has the following syntax -
 public void requestDestroyed(ServletRequestEvent event)   


Example -

This basic exmple will show you how to create and use ServletRequestListener.

1- index.html or index.jsp - This will be your default page.
<!DOCTYPE html>

<html>
    <head>
        <title>ServletRequestListener Demo</title>
    </head>
    <body>
        <a href="First">Click Here</a>
    </body>
</html>        

2- Create a package named serv and within that create a servlet named First.
First Servlet -

package servs;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;


public class First extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       try
       {
           PrintWriter out = response.getWriter();
            out.println("<h1>Welcome to First Servlet</h1>");
       }
       catch(Exception ex)
       {
           System.out.println(ex);
       }
    }
}        

3- Create a package named listeners and within that create a class named MyListener1 that will work as a listener -

package listeners;

import javax.servlet.*;

public class MytListener1 implements ServletRequestListener 
{
   `
    public void requestInitialized(ServletRequestEvent sre) 
    {
        System.out.println("Request came in");
    }
    public void requestDestroyed(ServletRequestEvent sre)
    {
       System.out.println("Request goes out");
    }
}    

4- web.xml - Add the information of servlet and listener in web.xml file -
<web-app>
    <listener>
        <listener-class>listeners.MytListener1</listener-class>
    </listener>
    <servlet>
        <servlet-name>First</servlet-name>
        <servlet-class>servs.First</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>First</servlet-name>
        <url-pattern>/First</url-pattern>
    </servlet-mapping>

</web-app>

5- Now run the project, it will send the request for index.html page, and as soon as request is about to come to the requested file, it will first invoke requestInitialized() method and when request is about to go out of scope of web-app, requestDestroyed() method invokes.

Browser Output -


Cosole output -


When you click on links given in index.html, again same process repeats -

Browser Output -


Cosole output -




WebListener Annotation -

javax.servlet.annotation.WebListener annotation is used to register a class as listener. In that case we do not need to define it in web.xml.
Make changes in the above example as follows -
MyListener1 -

package listeners;

import javax.servlet.*;
import javax.servlet.annotation.*;

@WebListener
public class MytListener1 implements ServletRequestListener 
{
    public void requestDestroyed(ServletRequestEvent sre) {
       System.out.println("Request goes out");
    }

    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("Request came in");
    }
}

web.xml - Now there is no need to add listener information in web.xml file.
<web-app>
    <servlet>
        <servlet-name>First</servlet-name>
        <servlet-class>servs.First</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>First</servlet-name>
        <url-pattern>/First</url-pattern>
    </servlet-mapping>

</web-app>    

Now run the project and it will run normally as in above example.