JavaWeb基础之Listener基本用法
2017-06-09 本文已影响38人
Acamy丶
用来监听特定对象的创建或销毁、属性的变化
1.监听器接口的分类
1.1、监听对象创建/销毁的监听器接口
Interface ServletRequestListener 监听request对象的创建或销毁
Interface HttpSessionListener 监听session对象的创建或销毁
Interface ServletContextListener 监听servletContext对象的创建或销毁
1.2、监听对象属性的变化
Interface ServletRequestAttributeListener 监听request对象属性变化: 添加、移除、修改
Interface HttpSessionAttributeListener 监听session对象属性变化: 添加、移除、修改
Interface ServletContextAttributeListener 监听servletContext对象属性变化
1.3、session相关监听器
Interface HttpSessionBindingListener 监听对象绑定到session上的事件
Interface HttpSessionActivationListener(了解) 监听session序列化及反序列化的事件
2.监听器的开发
2.1 步骤:
2.1.1.写一个普通java类,实现相关接口;
2.1.2.配置(web.xml)
2.2 实例
A.监听request对象的创建或销毁
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class MyRequestListener implements ServletRequestListener{
// 对象销毁
@Override
public void requestDestroyed(ServletRequestEvent sre) {
// 获取request中存放的数据
Object obj = sre.getServletRequest().getAttribute("cn");
System.out.println(obj);
System.out.println("MyRequestListener.requestDestroyed()");
}
// 对象创建
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("MyRequestListener.requestInitialized()");
}
}
<!-- 监听request对象创建、销毁 -->
<listener>
<listener-class>cn.acamy.a_life.MyRequestListener</listener-class>
</listener>
B. 监听ServletContext对象创建或销毁
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("MyServletContextListener.contextDestroyed()");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("1..........MyServletContextListener.contextInitialized()");
}
}
<!-- 监听servletContext对象创建、销毁 -->
<listener>
<listener-class>cn.acamy.a_life.MyServletContextListener</listener-class>
</listener>
C.监听Session对象创建、销毁
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener{
// 创建
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("MySessionListener.sessionCreated()");
}
// 销毁
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("MySessionListener.sessionDestroyed()");
}
}
<!-- 监听session对象创建、销毁 -->
<listener>
<listener-class>cn.itcast.a_life.MySessionListener</listener-class>
</listener>
D.监听session对象属性的变化
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class MySessionAttrListener implements HttpSessionAttributeListener {
// 属性添加
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
// 先获取session对象
HttpSession session = se.getSession();
// 获取添加的属性
Object obj = session.getAttribute("userName");
// 测试
System.out.println("添加的属性:" + obj);
}
// 属性移除
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("属性移除");
}
// 属性被替换
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
// 获取sesison对象
HttpSession session = se.getSession();
// 获取替换前的值
Object old = se.getValue();
System.out.println("原来的值:" + old);
// 获取新值
Object obj_new = session.getAttribute("userName");
System.out.println("新值:" + obj_new);
}
}
E.监听此对象绑定到session上的过程
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class Admin implements HttpSessionBindingListener {
private int id;
private String name;
public Admin() {
super();
}
public Admin(int id, String name) {
super();
this.id = id;
this.name = name;
}
// 构造函数
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 对象放入session
@Override
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("Admin对象已经放入session");
}
// 对象从session中移除
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("Admin对象从session中移除!");
}
}
注意:
因为监听的对象是自己创建的对象,不是服务器对象,所以不用再进行web.xml配置