To implement a SessionListener in java we implement the javax.servlet.http.HttpSessionListener interface.
Here is our implementation of the HttpSessionListener
CountSessionListener
package servletpackage;
import java.sql.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CountSessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSessionEvent event)
{
HttpSession session=event.getSession();
try
{
String id=session.getId();
java.util.Date d=new java.util.Date();
System.out.println("Session Created with ID=" + id + " at time " + d);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public void sessionDestroyed(HttpSessionEvent event)
{
HttpSession session=event.getSession();
try
{
String id=session.getId();
java.util.Date d=new java.util.Date();
System.out.println("Session Destroyed with ID=" + id + " at time " + d);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
import java.sql.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CountSessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSessionEvent event)
{
HttpSession session=event.getSession();
try
{
String id=session.getId();
java.util.Date d=new java.util.Date();
System.out.println("Session Created with ID=" + id + " at time " + d);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public void sessionDestroyed(HttpSessionEvent event)
{
HttpSession session=event.getSession();
try
{
String id=session.getId();
java.util.Date d=new java.util.Date();
System.out.println("Session Destroyed with ID=" + id + " at time " + d);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
To redirect the Standard Output in Apache Tomcat we use the logging tab in the Tomcat Monitor.
To install this session listener the following entries are required in the web.xml file.
web.xml
<web-app>
<listener>
<listener-class>servletpackage.CountSessionListener</listener-class>
</listener>
<session-config>
<session-timeout>2</session-timeout>
</session-config>
</web-app>
<listener>
<listener-class>servletpackage.CountSessionListener</listener-class>
</listener>
<session-config>
<session-timeout>2</session-timeout>
</session-config>
</web-app>
The underlined portion is the session listener configuration. We have also set a session timeout of 2 minutes.
To test the listener create two jsp pages. hello.jsp, and bye.jsp. hello.jsp creates a session while bye.jsp destroys it.
Here are the two files:-
hello.jsp
<%
request.getSession(true);
%>
request.getSession(true);
%>
bye.jsp
<%
session.invalidate();
%>
session.invalidate();
%>
Open the two files and refresh them alternately in the browser.
Here is the output in the out.txt file where the System.out is redirected.2012-04-12 14:01:32 Commons Daemon procrun stdout initialized
Session Created with ID=EA0AEFF5335E7F8E9B59F99FBDA5157D at time Thu Apr 12 14:01:44 IST 2012
Session Destroyed with ID=EA0AEFF5335E7F8E9B59F99FBDA5157D at time Thu Apr 12 14:02:05 IST 2012
Session Created with ID=2BE4BD1A6EFAA45785F63EB5991B46D1 at time Thu Apr 12 14:24:04 IST 2012
Session Destroyed with ID=2BE4BD1A6EFAA45785F63EB5991B46D1 at time Thu Apr 12 14:24:04 IST 2012
Session Created with ID=2BFF301ED2C87299CB0E75F51323340D at time Thu Apr 12 14:24:06 IST 2012