• 监听器(Linstener)简介
监听器是一个实现特定接口的普通Java程序,这个程序专门用于监听另一个Java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法立即被执行
HttpSessionBindingListener不用配置也可使用。
编写监听器的步骤
• 编写实现类
• 在web.xml中进行部署
• 编写测试页面
两种配置方式
注解配置
xml文件配置
• 与ServletContext相关监听器
全局对象:application范围对象
• 单个Web站点的资源都共享一个javax.servlet.ServletContext类的实体。
通过该对象可以存取应用程序的全局对象以及初始化阶段的变量
• 全局对象即为Application范围对象,其生命周期从容器启动至容器关闭。
初始阶段的变量是指在web.xml中,由<contextparam>元素设定的变量,该变量的范围是Application范围
ServletContextListener接口
模拟参数变化,配合下面两个代码块使用
package net.lww.servlet;
@WebServlet("/TestContextServlet")
public class TestContextServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestContextServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("Context服务器正常启动");
//模拟Context参数的变化
ServletContext context=getServletContext();
//新建属性
context.setAttribute("uname", "unameValue1");
//修改属性
context.setAttribute("uname", "unameValue2");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
• 实现了该接口的程序,当JavaWeb应用程序启动时,会自动开始监听工作
• 首先调用contextInitialized()方法接收对应的ServletContextEvent事件
• 当应用从容器中移除时,会自动调用contextDestroyed()方法
• 以上两个方法都会接收到ServletContextEvent事件对象,
该对象可以调用getServletContext()方法取得ServletContext对象(全局对象)
package net.lww.listener;
//@WebListener
//通过web.xml进行配置
public class CustomServletContextListener implements ServletContextListener {
@Override//初始化方法,服务器初始化,对象创建就调用
public void contextInitialized(ServletContextEvent sce) {
ServletContext context=sce.getServletContext();
System.out.println("Context作用域对象初始化");
//通常做一些全局初始化与预处理操作
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Context作用域对象销毁");
//通常做资源的释放等收尾操作
}
public CustomServletContextListener() {
}
}
ServletContextAttributeListener接口
• 实现该接口的程序,能够监听Application范围的变化,例
如:当有对象加入全局范围时,该程序会被调用
package net.lww.listener;
@WebListener//可以检测属性的变化
public class CustomServletContextAttributeListener implements ServletContextAttributeListener {
public CustomServletContextAttributeListener() {
}
public void attributeAdded(ServletContextAttributeEvent scae) {
//获取ServletContext对象
ServletContext context=scae.getServletContext();
//获得新建属性的数值
String name=scae.getName();
String value=(String) scae.getValue();
System.out.println(name+"新建"+value);
}
public void attributeRemoved(ServletContextAttributeEvent scae) {
//获得删除的属性值
//。。。。。。。。。。
}
public void attributeReplaced(ServletContextAttributeEvent scae) {
//获取修改的属性的名称,获得的是修改之前的旧数值
String name=scae.getName();
String value=(String) scae.getValue();
System.out.println(name+"修改前"+value);
//获取修改属性的新值
ServletContext context=scae.getServletContext();
String newValue=(String) context.getAttribute(name);
System.out.println(name+"修改后"+newValue);
}
}
• 与HttpSession相关监听器
HttpSessionListener
HttpSessionListener监听Session对象的创建与销毁,当
有Session对象产生或销毁时,会自动调用
sessionCreated()或sessionDestroyed()两个方法
• 与ServletRequest相关监听器
• HttpSessionListener接口与
HttpSessionActivationListener接口都使用
HttpSessionEvent事件对象
• HttpSessionEvent类主要的方法:
– getSession()
HttpSessionActivationListener接口
• 该接口主要用于:同一个Session转移到不同JVM的情形(如:负载均衡,这些
JVM可以在同一台机器或分散在网络中的多台机器)
• 当Session被储存起来,并且等待转移至另一个JVM,这段时间称为失效状态
(Passivate),若Session中的属性对象实现HttpSessionActivationListener接
口时,Container会自动调用sessionWillPassivate()方法通知该对象的
Session已变成失效状态
• 当Session被转移至其他JVM之后,它又成为有效状态(Activate),此时
Container会自动调用sessionDidActivate()方法通知该对象的Session已变成
有效状态
HttpSessionAttributeListener
HttpSessionAttributeListener会监听Session范围的变化,功能与ServletContextAttributeListener接口类似,包含三个方法
– attributeAdded()
– attributeReplaced()
– attributeRemove()
HttpSessionBindingEvent事件•
HttpSessionBindingEvent事件主要有三个方法
– getName()
– getSession()
– getValue()
HttpSessionBindingListener
实现HttpSessionBindingListener接口后,只要有对象加
入Session范围或从Session范围中移除时,容器会分别自
动调用下面两个方法:
– valueBound(HttpSessionBindingEvent e)
– valueUnbound(HttpSessionBindingEvent e)
HttpSessionBindingListener接口是唯一不需要在
web.xml中设定的Listener
HttpSessionBindingListener
自定义实现HttpSessionBindingListener接口的类
• 实例化监听器类的对象
• 将该对象添加到Session中
HttpSessionBindingListener
• HttpSessionAttributeListener使用的事件与
HttpSessionBindingListener使用的事件相同:
HttpSessionBindingEvent
• HttpSessionAttributeListener与
HttpSessionBindingListener的不同在于:
– 前者监听Web站点所有Session范围的变化
– 后者只监听Session范围内实现了HttpSessionBindingListener
接口的对象的创建与销毁
接口方法列表
• 与ServletRequest相关监听器
当有请求产生或销毁,会自动调用该接口实现的requestInitialized()或requestDestroyed()方法
• 该接口使用ServletRequestEvent事件
ServletRequestListener
ServletRequestAttributeListener
• 该接口监听Request范围的变化,有三个主要方法:
– attributeAdded()
– attributeReplaced()
– attributeRemoved()
• 使用ServletRequestAttributeEvent事件
ServletRequestAttributeListener
监听器的应用
• ServletContext范围的监听器可以进行一些初始化的动作,
如:当Web应用启动的时候进行全局配置
• Session范围的监听器对一个会话过程(与客户端关联)中所产生的事件进行响应,
可以对客户端信息的变化进行跟踪
• Request范围的监听器可以监听用户的每次请求