/**
* 监听器的使用:
* 作用:
* 监听作用域对象request、session、application的创建、销毁和内容的改变
* 使用:
* 创建一个实现了指定接口的java类
* 监听request--->ServletRequestListener 监听request对象的创建和销毁
* requestInitialized(ServletRequestEvent sre)//创建
* requestDestroyed(ServletRequestEvent sre)//销毁
* 注意:形参可以获取监听的request对象
* sre.getServletRequest();
* 监听request--->ServletRequestAttributeListener 监听request作用域数据的变更的
* attributeAdded(ServletRequestAttributeEvent srae)
* attributeReplaced(ServletRequestAttributeEvent srae)
* attributeRemoved(ServletRequestAttributeEvent srae)
* 注意:形参可以获取被监听的数据
* srae.getName()获取监听数据的键
srae.getValue()获取监听数据的值
* 监听session--->HttpSessionListener 监听session的创建和销毁
* sessionCreated(HttpSessionEvent se)
* sessionDestroyed(HttpSessionEvent se)
* 注意:形参可以获取被监听的session对象
* se.getSesstion();
* 监听session--->HttpSessionAttributeListener 监听session作用域数据的变更
* attributeAdded(HttpSessionBindingEvent event)
* attributeRemoved(HttpSessionBindingEvent event)
* attributeReplaced(HttpSessionBindingEvent event)
* 注意:形参可以获取被监听的数据
* event.getName() 获取数据的键名
* event.getValue() 获取数据的值
* 监听application--->ServletContextListener 监听application的初始化和销毁
* contextInitialized(ServletContextEvent sce) 初始化 服务器启动
* contextDestroyed(ServletContextEvent sce) 销毁 服务器关闭
* 注意:
* 形参可以获取当前application对象
* sce.getServletContext();
* 监听application--->ServletContextAttributeListener 监听application作用域的数据变更
* attributeAdded(ServletContextAttributeEvent event)
* attributeRemoved(ServletContextAttributeEvent event)
* attributeReplaced(ServletContextAttributeEvent event)
* 注意:
* 形参可以获取当前监听的数据
* event.getName() 获取数据的键名
* event.getValue() 获取数据的值
* 在web.xml中配置监听器
* <listener>
<listener-class>cn.edu.lingnan.listener.MyListener</listener-class>
</listener>
案例:
统计当前在线人数。
统计网页浏览次数。
* @author Administrator
*
*/
public class MyListener implements ServletRequestListener, ServletRequestAttributeListener,
HttpSessionListener,HttpSessionAttributeListener,ServletContextListener,ServletContextAttributeListener {
//request对象销毁
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("MyListener.requestDestroyed(我被销毁了)");
}
//request对象创建
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("MyListener.requestInitialized(我被创建了)");
}
//request作用域数据的销毁
public void attributeRemoved(ServletRequestAttributeEvent srae) {
}
//request作用域数据的添加
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("MyListener.attributeAdded( request作用域数据增加了)--"+srae.getName()+":"+srae.getValue());
}
//request作用域数据被修改
public void attributeReplaced(ServletRequestAttributeEvent srae) {
System.out.println("MyListener.attributeReplaced( request作用域数据被修改了)--"+srae.getName()+":"+srae.getValue());
}
/*-----------------------------------------------------------------------------------------------------------------*/
//监听session的创建
public void sessionCreated(HttpSessionEvent se) {
System.out.println("MyListener.sessionCreated(创建了session)");
}
//监听session的销毁
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("MyListener.sessionDestroyed(session被销毁了)");
}
//监听session数据的变更
public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println("MyListener.attributeAdded(session中增加了一条数据)");
}
public void attributeRemoved(HttpSessionBindingEvent event) {
}
public void attributeReplaced(HttpSessionBindingEvent event) {
}
/*---------------------------------------------------------------------------------------*/
//监听application的销毁
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("MyListener.contextDestroyed(application被销毁了)");
}
//监听application的创建
public void contextInitialized(ServletContextEvent sce) {
System.out.println("MyListener.contextInitialized(application被创建了)");
}
public void attributeAdded(ServletContextAttributeEvent event) {
System.out.println("MyListener.attributeAdded(application增加了一条数据)");
}
public void attributeRemoved(ServletContextAttributeEvent event) {
}
public void attributeReplaced(ServletContextAttributeEvent event) {
}
}