Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:
ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。
ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
package com.xling.tools.application;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class AppListener
extends HttpServlet implements ServletContextListener,
ServletContextAttributeListener, HttpSessionListener,
HttpSessionAttributeListener, ServletRequestListener,
ServletRequestAttributeListener {
//Notification that the web module is ready to process requests
int userCount = 0;
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Application Run!");
}
//Notification that the servlet context is about to be shut down
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Application ended!");
}
//Notification that a new attribute has been added to the servlet context
public void attributeAdded(ServletContextAttributeEvent scab) {
}
//Notification that an attribute has been removed from the servlet context
public void attributeRemoved(ServletContextAttributeEvent scab) {
}
//Notification that an attribute of the servlet context has been replaced
public void attributeReplaced(ServletContextAttributeEvent scab) {
}
//Notification that a session was created
public void sessionCreated(HttpSessionEvent se) {
userCount ++;
System.out.println(userCount + " users on line!");
}
//Notification that a session was invalidated
public void sessionDestroyed(HttpSessionEvent se) {
userCount --;
System.out.println(userCount + " users on line");
}
//Notification that a new attribute has been added to a session
public void attributeAdded(HttpSessionBindingEvent se) {
}
//Notification that an attribute has been removed from a session
public void attributeRemoved(HttpSessionBindingEvent se) {
}
//Notification that an attribute of a session has been replaced
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println(se.getName() + " : " + se.getValue() + " " + se.getSession().getAttribute(se.getName()));
}
//Notification that a request was created
public void requestInitialized(ServletRequestEvent sre) {
}
//Notification that a request was invalidated
public void requestDestroyed(ServletRequestEvent sre) {
}
//Notification that a new attribute has been added to a request
public void attributeAdded(ServletRequestAttributeEvent srae) {
}
//Notification that an attribute has been removed from a request
public void attributeRemoved(ServletRequestAttributeEvent srae) {
}
//Notification that an attribute of a request has been replaced
public void attributeReplaced(ServletRequestAttributeEvent srae) {
}
}