Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article
Stardeveloper RSS Feed
Hosted by Securewebs.com
 
Home : J2EE : Servlets : J2EE Web Application Events
 
RSS - Read full length articles at Stardeveloper using Stardeveloper RSS Feed RSS

J2EE Web Application Events

by Faisal Khan. Follow Faisal Khan on Twitter Follow Faisal Khan on Facebook

Overview :
Web application events are new in Servlet 2.3 specification. They give you greater degree of control over your web application. In this article we will study two important application events :

  • Application startup and shutdown
  • Session creation and invalidation

As their names suggest, application startup event occurs when your web application is first loaded and started by the Servlet container and application shutdown event occurs when the web application is shutdown.

Session creation event occurs everytime a new session is created on the server and similarly session invalidation event occurs everytime a session is invalidated. To make use of these web application events and to do something useful you'll have to create and make use of special "listener" classes. From here onwards, we'll look at what these listener classes are how you can use them.

Listener Classes :
These are simple Java classes which implement one of the two following interfaces :

  • javax.servlet.ServletContextListener
  • javax.servlet.http.HttpSessionListener

If you want your class to listen for application startup and shutdown events then implement ServletContextListener interface. If you want your class to listen for session creation and invalidation events then implement HttpSessionListener interface.

Let's see what are the different methods of these interfaces which you'll have to implement.

ServletContextListener :
This interface contains two methods :

  • public void contextInitialized(ServletContextEvent sce);
  • public void contextDestroyed(ServletContextEvent sce);

Once you implement any interface you have to implement all of it's methods. So you if you want to make use of application startup and shutdown events, then create a Java class and implement ServletContextListener interface. An example of such a class is as follows :

/*
	File : ApplicationWatch.java
*/

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;

public class ApplicationWatch implements ServletContextListener	{

	public static long applicationInitialized =	0L;

	/* Application Startup Event */
	public void	contextInitialized(ServletContextEvent ce) {
		applicationInitialized = System.currentTimeMillis();
	}

	/* Application Shutdown	Event */
	public void	contextDestroyed(ServletContextEvent ce) {}
}

In the code above, a Java class; ApplicationWatch, implements ServletContextListener interface. It implement both of it's methods but it really uses only one of them and the second method's body remains empty. This class notes down the time of application startup in a public static variable which can be called from other application classes to know what was the last time this application was started.

I'll explain how to tell the application server that we have this listener class and we want to be told of these application events in a moment, but first let's see what are the different methods of HttpSessionListener interface.

HttpSessionListener :
This interface also contains just two methods, for session creation and invalidation events respectively :

  • public void sessionCreated(HttpSessionEvent se);
  • public void sessionDestroyed(HttpSessionEvent se);

Like what we did in the case of ApplicationWatch above, we'll have to create a Java class and implement HttpSessionListener interface. An example of such a class is as follows :

/*
	File : SessionCounter.java
*/

import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;

public class SessionCounter	implements HttpSessionListener {

	private	static int activeSessions =	0;

	/* Session Creation	Event */
	public void	sessionCreated(HttpSessionEvent	se)	{
		activeSessions++;
	}

	/* Session Invalidation	Event */
	public void	sessionDestroyed(HttpSessionEvent se) {
		if(activeSessions >	0)
			activeSessions--;
	}

	public static int getActiveSessions() {
		return activeSessions;
	}
}

In the code above, SessionCounter class implements HttpSessionListener to count the number of active sessions. We will learn more about counting active users in a separate article in more detail.

Ok, we have learned what are web application events, what interfaces are available to us and have also seen examples of classes implementing those interfaces. Let's see how to tell the application server about these listener classes.

Web.xml :
We do that by putting classpath of these classes in /WEB-INF/web.xml file under special <listener> tags. An example of such a web.xml file is given below :

<!-- Web.xml -->
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
	PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
	"http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">

<web-app>

	<!-- Listeners -->
	<listener>
		<listener-class>
		com.stardeveloper.web.listener.SessionCounter
		</listener-class>
	</listener>
	<listener>
		<listener-class>
		com.stardeveloper.web.listener.ApplicationWatch
		</listener-class>
	</listener>

</web-app>

As shown above it is quite easy to declare listener classes in web.xml files. Now every time the server starts or shutdown, a session is created or destroyed, your classes will be told as their specific event methods will be called. It is that simple!

Summary :
In this article we learned what are web application events and how we can make use of these events by creating special "listener" classes. We then created two classes which implemented ServletContextListener and HttpSessionListener interfaces respectively. We also learned how to declare these listener classes in web.xml file by using special <listener> and <listener-class> tags.


 ( No Further Pages )

Related Articles
  1. Counting Active Users using JSP

Comments/Questions ( Threads: 1, Comments: 1 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. How use SessionListener class in a JSP?

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.