In this article, we are going to understand how to create a listener class which listens to the event of adding, replace or removing of ServletRequest attribute in the web application.
For this, we are going to create a webpage which asks the user to enter his name and password in a form and click the submit button.
On submitting the form, the user request goes to the requested Servlet class where the operations such adding, replace or removing of ServletRequest attribute
generates the events and calls the listener class,
listening to an such events.
To do this, we are going to create a Listener class by implementing ServletRequestAttributeListener interface
and by providing implementation of its methods.
ServletRequestAttributeListener is a part of the java.util.* package.
ServletRequestAttributeListener methods
Let's take a look at the methods of ServletRequestAttributeListener interface.
This method receives notification about an attribute has been deleted from the ServletRequest.
Creating a webpage which calls the Servlet
First we execute the webpage containing the form, asking the user to enter his/her name and city and press submit button.
On submitting the form, the user request goes to the Servlet class, where the operations of adding, replace and removing the ServletRequest attributes
will generate the events which will eventually call the listener class,
listening to the such events.
Form1.jsp
<html>
<head>
<title> ServletRequest Demo </title>
</head>
<body>
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" />
</form>
</body>
</html>
Creating a listener class to listen to an event of user request
We are creating a Servlet class by extending GenericServlet abstract class.
In this Servlet class, we are just sending a message to the user, asking to open at the Tomcat Web Server window for the information
about the events such as adding, replace and removing the request attributes.
MyServlet1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class MyServlet1 extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<b><i>Adding, replace and removing</i> the request Attributes.</b></br> </br> </br> </br>");
out.println("<b>Please look at the Tomcat Server Window for more details</b>");
//Setting Request Attributes to be shared between multiple servlets
request.setAttribute("Name", request.getParameter("username"));
request.setAttribute("City", request.getParameter("cityname"));
//Replace the attribute Name with a new value
request.setAttribute("Name", "Sky Gazer");
//Removing the attribute Name by giving it a null value
request.setAttribute("Name", null);
}
}
Next, we are creating a Listener class, listening to the events i.e. adding, replace and removing the request attributes
generated in the application, by calling getName() and getValue() method of ServletRequestAttributeEvent class.
The information about request is displayed
on the Tomcat Web Server window, as we are using System.out.println() method.
RequestAttributeListener1.java
import java.io.*;
import javax.servlet.*;
import java.util.*;
public class RequestAttributeListener1 implements ServletRequestAttributeListener
{
public void attributeAdded(ServletRequestAttributeEvent srae)
{
System.out.println("A new request attribute is added");
System.out.println("Name of this attribute " + srae.getName());
System.out.println("Value of this attribute " + srae.getValue());
System.out.println();
System.out.println();
}
public void attributeRemoved(ServletRequestAttributeEvent srae)
{
System.out.println("A request attribute is removed");
System.out.println("Name of this attribute " + srae.getName());
System.out.println("Value of this attribute " + srae.getValue());
System.out.println();
System.out.println();
}
public void attributeReplaced(ServletRequestAttributeEvent srae)
{
System.out.println("A request attribute is replaced");
System.out.println("Name of this attribute " + srae.getName());
System.out.println("Value of this attribute " + srae.getValue());
System.out.println();
System.out.println();
}
}
Advertisement
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-
Project Folder - We have created a project folder named ServletRequestAttributeListenerEx within
the webapps folder of Tomcat installation folder. This folder contains the Servlet class(.java) file and WEB-INF folder.
WEB-INF - Within your project folder, you create another folder named WEB-INF and this folder should contain the deployment descriptor file(web.xml) of your Java Servlet program.
classes - Within the WEB-INF folder you should create a folder named classes. This folder should contain the compiled(.class) form of your Java Servlet class.
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 -
In deployment descriptor file, <listener> tag has a child tag <listener class> :
<listener-class> tag is used to specify the full qualified name of the Listener class and in our example this class is named RequestAttributeListener1.
<servlet> tag has two child tags <servlet-name> and <servlet-class> :
<servlet-name> tag is used to specify a unique name for our Servlet class, we have given it a unique name MyGenericServlet.
<servlet-class> tag is used to specify the full qualified name of the Servlet class and in our example our Servlet class is named MyServlet1.
Note
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 -
You could set the classpath by entering this command at the Command Prompt, but this only temporarily sets the classpath to the path of servlet-api.jar file. If you restart your system, you will have to set the classpath again.
set classpath=C:\apache-tomcat-9.0.2\lib\servlet-api.jar
In order to set the classpath permanently, you need to set your classpath system variables. A simple tutorial on how to permanently set the classpath system variables, you may read
How to permanently set the classpath system variable
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 and the Listener class file.
First we execute the webpage containing the form, asking the user to enter the name and city and press the submit button.
As the user submits the form,
the user request goes to the Servlet class, where the operations of adding, replace and removing the request attributes
will generate the events which will eventually call the listener class(RequestAttributeListener1.java),
listening to the such events.
Hence, you will see the Servlet class in execution and displaying the window with the following message, asking the user to check the
Tomcat Server Window to check details about the events generated in the web application.
And, eventually the listener class displays the information on the Tomcat Web Server window about the events i.e. adding, replace and removing
the request attributes
generated in the web application.