< Prev
Next >


In this article, we are going to understand how to create a specific type of listener class. This listener class will listen to the events of the arrival of a user request which calls a servlet, and when this request is finally served by a web application.

For this, we are going to create a webpage which asks the user to enter his name and city in a form and click the submit button. On submitting this form, the user request not only calls the requested Servlet class but also calls the listener class, which is listening to an event of the arrival of a servlet request.

Within the listener class, we could also access the information about the user request by using the ServletRequest object. This information about the user request could be -
Note: We will create a listener class by implementing the ServletRequestListener interface and by providing the implementation of its methods. The ServletRequestListener interface is a part of the java.util.* package.

ServletRequestListener methods

Let's take a look at the methods of ServletRequestListener interface.

Methods Description
void requestInitialized(ServletRequestEvent sre) This method receives notification about the arrival of a new user request.
void requestDestroyed(ServletRequestEvent sre) This method receives notification when the request has been served.

Creating a webpage which calls the Servlet

Before creating our listener class, we will create a webpage which contains a form to allow a user to enter his/her name, city and press the submit button. On submitting the form, the user request not only calls a Servlet class but also a listener class, which is listening to an event of arrival of a new user request.


<title> ServletRequest Demo </title>

Please enter your details :

<form action = "MyServlet">
Name : <input type = "text"   name = "username" />
City : <input type = "text"   name = "cityname" /> <br/>

<input type = "submit" value = "submit" />



Creating a Servlet

Directory Structure of Servlet files

The diagram above depicts how to arrange the Servlet files in a specific directory structure, as per Java Servlet Specification-

Creating the Deployment Descriptor file

As per the Java Servlet specifications, every web application based on Servlet must have a Deployment Descriptor file(an XML file) named web.xml. So, let's create one -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""

  <display-name>Welcome tomcat</display-name>
     Welcome tomcat

	 <listener-class>RequestListener1 </listener-class>





The child tag <servlet-name> of <servlet> tag is matched with the <servlet-name> child tag of <servlet-mapping>. The <url-pattern> child tag is used to specify the URL to access Servlet and we have named this URL MyServlet.

Setting the classpath

Much of the support for developing the web applications based on the Java Servlet technology does not come with the core Java. Hence, in order to compile the Servlet programs, we have to set the classpath to a jar file named servlet-api.jar.

This jar file provides all the classes that are required for the Servlet programming and it comes within the lib Folder of Tomcat installation folder.

For example, in our case we have installed Tomcat Web Server within the C: Drive, hence the path to our lib folder containing the servlet-api.jar is - C:\apache-tomcat-9.0.2\lib

There are two ways to set the classpath -

Compiling the Servlet class

After setting the classpath, you need to compile the Servlet class and the Listener class by entering the command at the folder where you've stored the Servlet class file.

javac -d WEB-INF/classes

javac -d WEB-INF/classes

Executing the Servlet

First we execute the webpage containing the form, asking the user to enter his/her name and city and press the submit button.

As user enters the asked details and submits the form, the request not only calls the Servlet, but also the listener class, which is listening to the event of arrival of a new request.

Hence, first you will see the Servlet class in execution, which will display a webpage with a message that asks the user to check the Tomcat Server Window for details about the user request, as shown below.

And, a subsequent call is made to the listener class named , which displays the information about the user request, the value of parameters such as username, cityname by using the ServletRequest on the Tomcat Web Server window. This listener class also notifies about the request being served and ended.

Please share this article -

< Prev
Next >
< Listener Interfaces
ServletRequestAttributeListener >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition