Listener



1. Listener

监听器也是一个接口,实现该接口的类会监听其他类的方法调用或属性改变,当发生被监听的事件后,监听器将执行指定的方法,而且不需要像监听器模式那样亲自向事件源注册,Tomcat服务器已经帮我们完成了

Listen涉及到了监听器模式,不了解的同学可以 看这里


1.1 监听的对象

ServletContext,HttpSession,ServletRequest


1.2 监听的事件

监听对象的创建和销毁,监听对象属性变化,监听Session内的对象


1.3 具体分类:


**监听对象的创建和销毁**

HttpSessionListener、ServletContextListener、ServletRequestListener,里面方法有:Destroyed(),Initialized()


**监听对象属性变化**

ServletContextAttributeListener、HttpSessionAttributeListener、ServletRequestAttributeListener,里面方法有:attributeAdded(),attributeRemoved(),attributeReplaced()


监听Session内的对象

HttpSessionActivationListener、HttpSessionBindingListener,实现这两个接口并不需要在web.xml文件中注册,因为是让里面的对象自己监听自己,并且对象需要实现序列化接口,里面方法有:valueBound(),valueUnbound(),sessionDidActivate(),sessionWillPassivate()


Web.xml配置

<listener>
  	<listener-class>listener.ListenerTest</listener-class>
</listener>




2. 测试


2.1 监听对象的创建和销毁

public class ListenerTest implements HttpSessionListener, ServletContextListener, ServletRequestListener {

	public void requestDestroyed(ServletRequestEvent arg0) {
		System.out.println("请求销毁了");
	}

	public void requestInitialized(ServletRequestEvent arg0) {
		System.out.println("请求初始化了");
	}

	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("容器销毁了");
	}

	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("容器初始化了");
	}

	public void sessionCreated(HttpSessionEvent arg0) {
		System.out.println("Session创建了");
	}

	public void sessionDestroyed(HttpSessionEvent arg0) {
		System.out.println("Session销毁了");
	}
容器初始化了
请求初始化了
Session创建了
# Session是在内存中,不在JVM里,所以看不到销毁
请求销毁了
容器销毁了

2.2 监听对象属性变化(这里举例Session)

//增加
httpSession.setAttribute("Session", "Howl");
	
//替换
httpSession.setAttribute("Session", "Howlet");
	
//移除
httpSession.removeAttribute("Session");
public class ListenerTest2 implements HttpSessionAttributeListener {

	public void attributeAdded(HttpSessionBindingEvent arg0) {
		System.out.println("Session属性增加了");
	}

	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		System.out.println("Session属性移除了");
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		System.out.println("Session属性替换了");
	}
}    
Session属性增加了
Session属性替换了
Session属性移除了

2.3 监听Session内的对象

public class UserBean implements Serializable, HttpSessionActivationListener, HttpSessionBindingListener {

	private String name;
	
	public UserBean(String name) {
		this.name = name;
	}

	public void valueBound(HttpSessionBindingEvent arg0) {
		System.out.println("绑定了对象");
	}

	public void valueUnbound(HttpSessionBindingEvent arg0) {
		System.out.println("移除了对象");
	}

	public void sessionDidActivate(HttpSessionEvent arg0) {
		HttpSession httpSession = arg0.getSession();
        System.out.println("活化了");
	}

	public void sessionWillPassivate(HttpSessionEvent arg0) {
		HttpSession httpSession = arg0.getSession();
        System.out.println("钝化了");
	}
}    
UserBean user = new UserBean("Howl");
	
request.getSession().setAttribute("user", user);
request.getSession().removeAttribute("user");
绑定了对象
<!-- 关闭服务器 -->
钝化了
<!-- 开启服务器 -->
活化了
移除了对象

作用

统计用户人数

监听用户上线与退出




参考Java3y

原文地址:https://www.cnblogs.com/Howlet/p/12047343.html