java left logo
java middle logo
java right logo

Home arrow Java Tutorials arrow Introduction to Servlet Listener using Eclipse
Main Menu
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog

Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews

Registered Users: 4115
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
Introduction to Servlet Listener using Eclipse E-mail
User Rating: / 265

Now, the most important thing is that we need to make the class “implements” the ServletContextListener. So here is my latest


Now, we have an error in our class. Why? Because once we try to implement the javax.servlet.HttpSessionListener interface, we need to define a few implementation methods for it. That’s why I mentioned earlier that it is the pre-defined interfaces. This is where our job will be started. You need to add two methods for it.

  1. public void sessionCreated(HttpSessionEvent arg0)
  2. public void sessionDestroyed(HttpSessionEvent arg0)

So here is my latest

public class SessionListener implements javax.servlet.http.HttpSessionListener {

  public void sessionCreated(HttpSessionEvent arg0) {
    System.out.println("Session is created");

  public void sessionDestroyed(HttpSessionEvent arg0) {
    System.out.println("Session is destroyed");

Okay, if you add one attribute to the session, the session will be created for you and it should print out “Session is created”. Once you invalidate the session, the session should be destroyed and “Session is destroyed” will be printed out.

Now, we need to add one more entry in our web.xml.


One thing you need to know that this listener entry MUST be before the <servlet> entry (if any). So here is my latest web.xml looks like. Yes, Listener only needs <listener></listener>

So this is how my latest web.xml looks like after implementing both ServletContextListener and HttpSessionListener.

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


Ok, actually we have completed our SessionListener. The only thing is that how to test it. There is one thing you can do. Create one JSP on your web application and we will assign some attributes to the sessions (adding attribute means that we are creating the Session). Create your JSP using the wizard and name it whatever you want. For my case, I would like to name it testSession.jsp.

Ok, now let’s add some codes to add attribute to the session. We will achieve it using Scriptlet. Scriptlet is basically Java codes that are embedded in our JSP. It helps us in creating our application dynamic. So please add below codes in your JSP.


	System.out.println("Adding attribute to Session...");
	request.getSession().setAttribute("test", "test");
	System.out.println("Adding attribute to Session completed!");
	System.out.println("Removing Session...");
	System.out.println("Removing Session completed!");		
	out.println("Successfully adding and removing attributes from Session");	

This scriptlet can be placed anywhere in your JSP. For this example, I can just put it within my <body></body> tag. If you are in the right track, it should look like below screenshot.


Let’s test it. Export your project into WAR files and deploy it to Tomcat. Access the testSession.jsp that we just created and carefully look at the Tomcat console.

As expected, when the first time Tomcat is loaded, our ServletContextListener is still executed.


Now, open your browser and let’s try to execute the testSession.jsp by typing the URL manually into your browser. In my case, it should be http://localhost:8080/ListenerWebApp/testSession.jsp.

This is my Tomcat console now.



Well, you should have successfully implemented both ServletContextListener as well as HttpSessionListener. These two listeners are the most widely used and I hope that by reading the tutorial, you have a clear way and objective when you are moving forward with J2EE programming. There are still much to be learnt and please do not worry as this is not hard. My suggestion is to try the sample in this tutorial and try to customize it. Your skills will be much improved by practicing more. There are also two additional Listeners that are not mentioned in this tutorial which are ServletContextAttributeListener and HttpSessionAttributeListener. They are just slightly different with our tutorial. Have a look at them and try them out.

You can find the Eclipse project for the tutorial source codes here.
The README file for the sources is available here.

 Related Tips

< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.

Name (required)

E-Mail (required)

Your email will not be displayed on the site - only to our administrator

Comment Enable HTML code : Yes No

java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008
Java is a trademark of Sun Microsystems, Inc.