jinfeng_wang

G-G-S,D-D-U!

BlogJava 首页 新随笔 联系 聚合 管理
  400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks
http://www.stardeveloper.com/articles/display.html?article=2001111901&page=1


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.



posted on 2008-03-18 17:02 jinfeng_wang 阅读(478) 评论(0)  编辑  收藏 所属分类: javaZZ

只有注册用户登录后才能发表评论。


网站导航: