监听器Listerer的作用是监听ServletContext、ServletContextAttribute、HttpSession、HttpSessionAttribute事件的监听。
具体包括监听:上下文初始化、上下文销毁、上下文属性增加、上下文属性删除、上下文属性替换、Session创建、Session销毁、向Session里添加属性、从Session里删除属性、Session里属性替换等等。
当发生以上事件时,就会触发监听器的执行,但是前提是自定义的监听器需要实现上面功能的接口。
监听器随web应用的启动而启动,只初始化一次,随web应用的停止而销毁。
监听器应用案例:
1、统计当前在线人员。
2、对象的初始化,因为监听器的初始化是在容器启动之前。例如:
使用spring框架,就需要在web.xml中配置spring的一个listener,在tomcat启动的时候就会初始化spring
初始化数据库连接
自定义的监听器可能实现的相关接口:
ServletContextListener:Servlet上下文
ServletContextAttributeListener:Servlet上下文属性
HttpSessionListener:Session
HttpSessionAttributeListener:Session属性操作
ServletContextListener接口的方法:
public void contextInitialized(ServletContextEvent event);//上下文初始化
public void contextDestroyed(ServletContextEvent event);//上下文销毁
ServletContextAttributeListener接口的方法:
public void attributeAdded(ServletContextAttributeEvent event);//增加属性
public void attributeRemoved(ServletContextAttributeEvent event);//删除属性
public void attributeReplaced(ServletContextAttributeEvent event);//替换属性
HttpSessoinListener接口的方法:
public void sessionCteated(HttpSessionEvent event);//session创建
public void sessionDestroyed(HttpSessionEvent event);//session销毁
HttpSessionAttributeListener接口的方法:
public void attributeAdded(HttpSessoinBindingEvent event);//增加属性
public void attributeRemoved(HttpSessoinBindingEvent event);//删除属性
public void attributeReplaced(HttpSessoinBindingEvent event);//替换属性
MyListener.java
package com.vmn;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionBindingEvent;
public class MyListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener{
public void contextInitialized(ServletContextEvent event){
System.out.println("contextInitialized");
}
public void contextDestroyed(ServletContextEvent event){
System.out.println("contextDestroyed");
}
public void attributeAdded(ServletContextAttributeEvent event){
System.out.println("context attributeAdded");
}
public void attributeRemoved(ServletContextAttributeEvent event){
System.out.println("context attributeRemoved");
}
public void attributeReplaced(ServletContextAttributeEvent event){
System.out.println("context attributeReplaced");
}
public void sessionCreated(HttpSessionEvent event){
System.out.println("sessionCreated");
}
public void sessionDestroyed(HttpSessionEvent event){
System.out.println("sessionDestroyed");
}
public void attributeAdded(HttpSessionBindingEvent event){
System.out.println("session attributeAdded");
}
public void attributeRemoved(HttpSessionBindingEvent event){
System.out.println("session attributeRemoved");
}
public void attributeReplaced(HttpSessionBindingEvent event){
System.out.println("session attributeReplaced");
}
}
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>testlistener</servlet-name>
<servlet-class>com.vmn.TestListener</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testlistener</servlet-name>
<url-pattern>/tl</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.vmn.MyListener</listener-class>
</listener>
</web-app>
14到16行定义了一个Listener:com.vmn.MyListener
当在web应用中的servlet中触发了MyListener.java中定义的那10个事件的时候,就会触发这10个方法的执行。
相关API:
javax.servlet.ServletContextEvent;
javax.servlet.ServletContextAttributeEvent;
javax.servlet.http.HttpSessionEvent;
javax.servlet.http.HttpSessionBindingEvent;