Servlet之监听器

五、监听器

引入:在Servlet规范中存在三大组件:Servlet接口,Listener接口、Filter接口,而监听器接口Listener是一种设计模式

(一)监听器相关设计模式

1.设计模式

*设计模式是指可以重复利用的解决方案

2.三类设计模式

*创建型
*结构性
*行为型

3.观察者设计模式

4.监听器设计模式

监听器设计模式是观察者设计模式的一种实现
这里的监听器对应的是观察者,而被监听对象则是指被观察者
当被监听对象的状态发生改变时,也需要通知监听器,监听器在收到通知后会做出相应改变

(二)监听器Listener

1.Servlet规范中的监听器

(1)Servlet规范中已经定义好了八个监听器接口,他们要监听的对象分别是request,session,servletContext对象
(2)触发监听器的事件是这三个对象的销毁和创建,它们域属性空间中属性的添加,删除,修改
(3)使用监听器需要在web.xml文件中对监听器进行注册(除了最后两个不用注册)

2.ServletRequestListener监听器

package Listener;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class MyRequestListener implements ServletRequestListener 
{
    
    
	//request对象被销毁时执行
	public void requestDestroyed(ServletRequestEvent sre) 
	{
    
    
		System.out.println("对象被销毁");
	}
	//request对象被初始化时执行
	public void requestInitialized(ServletRequestEvent sre) 
	{
    
    
		System.out.println("对象被初始化");
	}
}

在web.xml中注册监听器,除了最后两个监听器不需要注册,其余都需要,以此类推

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  	<!-- 注册监听器 -->
	<listener>
		<listener-class>Listener.MyRequestListener</listener-class>
	</listener>
</web-app>

3.ServletRequestAttributeListener监听器

package Listener;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;

public class MyRequestAttributeListener implements ServletRequestAttributeListener
{
    
    
	//当向Request域中添加属性时,会触发该方法
	public void attributeAdded(ServletRequestAttributeEvent srae) 
	{
    
    
		System.out.println("在request域中添加了一个属性");
		//获取添加属性的名称和值
		System.out.println("添加的属性名为:" + srae.getName());
		System.out.println("添加的属性值为:" + srae.getValue());
	}
	//当向Request域中移除属性时,会触发该方法
	public void attributeRemoved(ServletRequestAttributeEvent srae) 
	{
    
    
		System.out.println("在request域中移除了一个属性");
		//获取删除属性的名称和值
		System.out.println("移除的属性名为:" + srae.getName());
		System.out.println("移除的属性值为:" + srae.getValue());
	}
	//当向Request域中重置属性时,会触发该方法
	public void attributeReplaced(ServletRequestAttributeEvent srae) 
	{
    
    
		System.out.println("在request域中修改了一个属性");
		//获取修改属性的名称和值(修改前的值)
		System.out.println("修改的属性名为:" + srae.getName());
		System.out.println("修改的属性值为:" + srae.getValue());
	}
}

触发事件的jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
	index page!<br>
	<%
		request.setAttribute("name", "zhangsan");
		request.setAttribute("name", "lisi");
		request.removeAttribute("name");
	%>
</body>
</html>

4.HttpSessionListener监听器

package Listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener 
{
    
    
	public void sessionCreated(HttpSessionEvent se) 
	{
    
    
		System.out.println("session被创建了");
	}
	public void sessionDestroyed(HttpSessionEvent se) 
	{
    
    
		System.out.println("session被销毁了");
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>notitle</title>
</head>
<body>
	<%-- 
		由于jsp本质是Servlet,jsp九大内置对象中就有session
		因此jsp页面一打开时就有session被创建
	 --%>
	index page<br>
	<%
		//销毁session
		session.invalidate();
	%>
</body>
</html>

5. HttpSessionAttributeListener监听器

package Listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class MySessionAttributeListener implements HttpSessionAttributeListener 
{
    
    
	public void attributeAdded(HttpSessionBindingEvent se) 
	{
    
    
		System.out.println("在session域中添加了一个属性");
		//获取添加属性的名称和值
		System.out.println("添加的属性名为:" + se.getName());
		System.out.println("添加的属性值为:" + se.getValue());
	}
	public void attributeRemoved(HttpSessionBindingEvent se) 
	{
    
    
		System.out.println("在session域中删除了一个属性");
		//获取删除属性的名称和值
		System.out.println("删除的属性名为:" + se.getName());
		System.out.println("删除的属性值为:" + se.getValue());
	}
	public void attributeReplaced(HttpSessionBindingEvent se) 
	{
    
    
		System.out.println("在session域中修改了一个属性");
		//获取修改属性的名称和值
		System.out.println("修改的属性名为:" + se.getName());
		System.out.println("修改的属性值为:" + se.getValue());
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	index page!<br>
	<%
		session.setAttribute("name", "zhangsan");
		session.setAttribute("name", "lisi");
		session.removeAttribute("name");
	%>
</body>
</html>

6.ServletContextListener监听器

package Listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener 
{
    
    
	//当ServletContext被销毁时会触发该方法
	public void contextDestroyed(ServletContextEvent sce) 
	{
    
    
		System.out.println("ServletContext被销毁");
	}
	//当ServletContext被初始化时会触发该方法
	public void contextInitialized(ServletContextEvent sce) 
	{
    
    
		System.out.println("ServletContext被创建");
	}
}

7. ServletContextAttributeListener监听器

package Listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class MyServletContextAttributeListener implements ServletContextAttributeListener 
{
    
    
	public void attributeAdded(ServletContextAttributeEvent scae)
	{
    
    
		System.out.println("ServletContext中添加了属性");
		System.out.println("属性名:" + scae.getName());
		System.out.println("属性值:" + scae.getValue());
	}
	public void attributeRemoved(ServletContextAttributeEvent scae) 
	{
    
    
		System.out.println("ServletContext中删除了属性");
		System.out.println("属性名:" + scae.getName());
		System.out.println("属性值:" + scae.getValue());
	}
	public void attributeReplaced(ServletContextAttributeEvent scae) 
	{
    
    
		System.out.println("ServletContext中重置了属性");
		System.out.println("属性名:" + scae.getName());
		System.out.println("属性值:" + scae.getValue());
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
	index page!
	<%
		application.setAttribute("address", "beijing");
		application.setAttribute("address", "shanghai");
		application.removeAttribute("address");
	%>
</body>
</html>

8.HttpSessionBindingListener监听器

package Listener;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/**
 * 定义实体类实现HttpSessionBindingListener接口
 * 当该实体类的对象(即Student对象)被放入session域或从session域中删除时就会触发以下两个重写的方法
 * 该监听器无需注册
*/
public class Student implements HttpSessionBindingListener 
{
    
    
	private String name;
	private int age;
	public Student() 
	{
    
    
		
	}
	public Student(String name, int age) 
	{
    
    
		this.name = name;
		this.age = age;
	}
	public String getName() 
	{
    
    
		return name;
	}
	public void setName(String name) 
	{
    
    
		this.name = name;
	}
	public int getAge()
	{
    
    
		return age;
	}
	public void setAge(int age) 
	{
    
    
		this.age = age;
	}
	//当前类对象被放入session时,会触发该方法
	public void valueBound(HttpSessionBindingEvent event) 
	{
    
    
		System.out.println("Student对象被放入session了");
	}
	//当前类对象被移除session时,会触发该方法
	public void valueUnbound(HttpSessionBindingEvent event) 
	{
    
    
		System.out.println("Student对象被移出session了");
	}
}
<%@ page pageEncoding="UTF-8" import="Listener.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
	index page!<br>
	<%
		Student s = new Student();
		session.setAttribute("s", s);
		session.removeAttribute("s");
	%>
</body>
</html>

9.HttpSessionActivationListener监听器

钝化:将内存中的数据写入硬盘
活化:将硬盘中的数据恢复到内存

package Listener;

import java.io.Serializable;

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
/**
 * 
 * 该实体类需要实现HttpSessionActivationListener和Serializable接口
 *
 */
public class Student implements HttpSessionActivationListener,Serializable
{
    
    
	private String name;
	private int age;
	public Student() 
	{
    
    
		
	}
	public Student(String name, int age) 
	{
    
    
		this.name = name;
		this.age = age;
	}
	public String getName() 
	{
    
    
		return name;
	}
	public void setName(String name) 
	{
    
    
		this.name = name;
	}
	public int getAge()
	{
    
    
		return age;
	}
	public void setAge(int age) 
	{
    
    
		this.age = age;
	}
	//当前类的对象被活化时会触发该方法
	public void sessionDidActivate(HttpSessionEvent se) 
	{
    
    
		System.out.println("session已被活化");
	}
	//当前类的对象被钝化时会触发该方法
	public void sessionWillPassivate(HttpSessionEvent se) 
	{
    
    
		System.out.println("session将被钝化");
	}
}
<%@ page pageEncoding="UTF-8" import="Listener.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		Student s = new Student();
		session.setAttribute("s", s);
	%>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_46841376/article/details/113525247