< Prev
Next >



HttpSessionListener



In this article, we are going to understand how to create a listener class which listens to an event when a session is created or destroyed in 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 the form, the user request goes to the requested Servlet class, which creates a new http session. This generates an event, which leads to the calling of the Listener class waiting for such event.

To do this, we are going to create a Listener class by implementing HttpSessionListener interface and by providing implementation of its methods.

HttpSessionListener is a part of the java.util.* package.




HttpSessionListener methods


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

Methods Description
void sessionCreated(HttpSessionEvent hse) This method receives notification when an session is created.
void sessionDestroyed(HttpSessionEvent hse) This method receives notification when a session is invalidated or destroyed.





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 a new http session will be created which will generate an event. This will eventually call the listener class, listening to such event.

Webpage1.htm
<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 a new Http Session




SessionListener1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.EventListener;
import java.util.*;


public class SessionListener1 implements HttpSessionListener
{


public void sessionCreated(HttpSessionEvent se)
{	
	System.out.println("A new Session is created");
}



public void sessionDestroyed(HttpSessionEvent se)
{
	System.out.println("A new Session is created");
}

}



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 -


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

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

  
<listener>
	 <listener-class>SessionListener1 </listener-class>
</listener>



<servlet>
 	<servlet-name>MyServlet</servlet-name>
	<servlet-class>MyServlet1</servlet-class>
</servlet>



<servlet-mapping>
	<servlet-name>MyServlet</servlet-name>
	<url-pattern>/Serv</url-pattern>
</servlet-mapping>

</web-app>


In deployment descriptor file, <listener> tag has a child tag <listener class> :




<servlet> tag has two child tags <servlet-name> and <servlet-class> :






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 -




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.

javac -d WEB-INF/classes MyServlet1.java


javac -d WEB-INF/classes SessionListener1.java





Executing the Servlet


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




As user enters the asked details and submits the form, the request not only goes to the Servlet and also to the listener class SessionListener1.java, listening to the creation of a new Http Session.

Hence, you will see the Servlet class in execution and displaying the information about the newly created session.




And, if you check the Tomcat Web Server window, you will also see a message notifying you about the event of creation of a new session within the web application. This message is displayed by the listener class.






Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< ServletRequestAttributeListener
ServletRequestWrapper >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2