Java监听器

时间:2021-11-16 06:18:41

监听器

1.概念

监听器:主要是用来监听特定对象的创建,属性的变化的!,本质上却是一个实现特定接口的普通java类!

对象分为自己创建自己使用的,和别人创建自己用的,自己创建的不需要监听,值需要取监听别人创建的对象。

Servlet中需要监听的对象有:

  • 用于http请求的Request域对象
  • 用于会话管理的session域对象
  • 用于Servlet服务的servletContext域对象

    根据监听域对象的不同,java中的监听分为三种,分别是:request监听器,session相关监听器,servletContext监听器

监听器接口:

注意下面的都是接口,接口下有实现的对象方法的类

1.监听对象/销毁的监听器接口

Interface ServletRequestListener

监听request对象的创建或销毁

Interface HttpSessionListener

监听Session对象的创建或销毁

Interface ServletContextListener

监听ServletContext对象的创建或销毁

2.监听对象属性的变化

Interface ServletRequestAttributeListener

监听request对象属性变化:添加,移除,修改

Interface HttpSessionAttributeListener

监听session对象属性变化:添加,移除,修改

Interface ServletContextAttributeListener

监听servletContext对象属性变化

3.Session相关监听器

Interface HttpSessionBindingListener

监听对象绑定到Session上的事件

Interface HttpSessionActivationListener(了解)

监听Session序列化及反序列化的事件

2.生命周期接口监听器

生命周期接口监听器:就是监听对象的创建,销毁事件的

监听器开发步骤:

1.写一个普通java类,实现相关接口

2.配置(web.xml),即部署

2.1.ServletRequestListener

request域对象:在http向服务器请求时创建,在服务器响应之后销毁


/**
* 监听request对象的创建和销毁
* Created by cenyu on 16-12-19.
*/ 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("request.......MyRequestListener.requestDestroyed");
} //对象创建
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("request.......MyRequestListener.requestInitialized");
}
}

2.2.HttpSessionListener

Session域对象:每次请求时都会创建Session,关闭浏览器或服务器不会销毁,可以等时效过去,或者手动删除



import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener; /**
* 测试监听session对象的创建,销毁
* Created by cenyu on 16-12-19.
*/
public class MySessionListener implements HttpSessionListener {
//创建
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("session......MySessionListener.sessionCreated"); } //销毁
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("session......MySessionListener.sessionDestroyed"); }
}

2.3.ServletContextListener

ServletContext域对象,在服务器启动时创建,在服务器关闭时销毁

package d_listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; /**
* 监听ServletContext对象的创建,销毁
* Created by cenyu on 16-12-19.
*/
public class MyServletContextListener implements ServletContextListener {
//创建
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("servlet......MyServletContextListener.contextInitialized");
} //销毁
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("servlet......MyServletContextListener.contextDestroyed");
}
}

以上三个监听器的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!--监听request对象创建,销毁-->
<listener>
<listener-class>d_listener.MyRequestListener</listener-class>
</listener> <!--监听session对象创建,销毁-->
<listener>
<listener-class>d_listener.MySessionListener</listener-class>
</listener> <!--监听ServletContext对象创建,销毁-->
<listener>
<listener-class>d_listener.MyServletContextListener</listener-class>
</listener> </web-app>

3.属性监听器

3.1.ServletRequestAttributeListener

监听request对象属性变化:添加,移除,修改

package d_listener;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener; /**
* 监听request对象属性变化:添加,移除,修改
* Created by cenyu on 16-12-20.
*/
public class MyRequestAttrListener implements ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent re) {
ServletRequest request = re.getServletRequest();
Object name = request.getAttribute("cn");
System.out.println("新增属性:"+name);
} @Override
public void attributeRemoved(ServletRequestAttributeEvent re) {
System.out.println("属性移除");
} @Override
public void attributeReplaced(ServletRequestAttributeEvent re) {
System.out.println("属性更改");
ServletRequest request = re.getServletRequest();
Object cn = request.getAttribute("cn");
System.out.println(cn); }
}

3.2.HttpSessionAttributeListener

监听session对象属性变化:添加,移除,修改

package d_listener;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent; /**
* 监听session对象属性变化:添加,移除,修改
* Created by cenyu on 16-12-19.
*/
public class MySessionAttrListener implements HttpSessionAttributeListener {
//增加属性
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
//先获取session对象
HttpSession session = se.getSession();
//获取添加的属性
Object obj = session.getAttribute("cn");
//测试
System.out.println("添加的属性,"+obj);
} //移除属性
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("属性移除"); } //更改属性
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
//获取Session对象
HttpSession session = se.getSession();
//获取替换前的值
Object obj1 = se.getValue();
System.out.println("原来的值:"+obj1);
//获取新值
Object obj2 = session.getAttribute("userName");
System.out.println("修改之后的新值:"+obj2);
}
}

3.3.ServletContextAttributeListener

监听servletContext对象属性变化

package d_listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener; /**
* 监听servletContext对象属性变化:添加,移除,修改
* Created by cenyu on 16-12-20.
*/
public class MyContextAttrListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent sb) {
ServletContext context = sb.getServletContext();
Object name = context.getAttribute("name");
System.out.println("context新增属性:"+name);
} @Override
public void attributeRemoved(ServletContextAttributeEvent sb) {
System.out.println("context值被移除"); } @Override
public void attributeReplaced(ServletContextAttributeEvent sb) {
ServletContext context = sb.getServletContext();
Object name = context.getAttribute("name");
System.out.println("context值被修改"+name);
}
}

总结:

先写类,实现接口,再配置

4.其他监听器:Session相关监听器

HttpSessionBindingListener

监听对象绑定/接触绑定到Session事件

步骤:

1.对象实现接口

2.把对象绑定/解除绑定到Session上就可以了,不用写配置

作用:

可以做上线提醒等功能

思考:

这个Session监听器,和上面的声明周期,属性监听器的区别?

Session监听器不用在web.xml配置,因为监听的对象是自己创建的对象,不是服务器对象

package d_listener;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener; /**
* 监听此对象绑定到Session上的过程,需要实现Session特定接口
* Created by cenyu on 16-12-20.
*/
public class Admin implements HttpSessionBindingListener{
private int id;
private String name; //对象放入Session
@Override
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("Admin对象已经放入Session");
} //对象从Session中移除
@Override
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("Admin对象从Session中移除");
} public Admin(int id, String name) {
this.id = id;
this.name = name;
} public Admin() {
super();
} 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;
} @Override
public String toString() {
return "Admin{" +
"id=" + id +
", name='" + name + '\'' +
'}';
} }

在jsp中做的测试,测试方法是添加Session,但是要添加对象才行,如下:

     //注意:添加的是new Admin()
session.setAttribute("userInfo",new Admin());
session.removeAttribute("userInfo");