一.监听器作用:
主要是用来监听特定对象的创建或销毁、属性的变化的! 是一个实现特定接口的普通java类!
Servlet中哪些对象需要监听?
request
/session
/servletContext
分别对应的是request监听器、session相关监听器、servletContext监听器
二.监听器(listener)概述
监听器接口:
- 监听对象创建/销毁的监听器接口
Interface ServletRequestListener
监听request对象的创建或销毁
Interface HttpSessionListener
监听session对象的创建或销毁
Interface ServletContextListener
监听servletContext对象的创建或销毁 - 监听对象属性的变化
Interface ServletRequestAttributeListener
监听request对象属性变化: 添加、移除、修改
Interface HttpSessionAttributeListener
监听session对象属性变化: 添加、移除、修改
Interface ServletContextAttributeListener
监听servletContext对象属性变化 - session相关监听器
Interface HttpSessionBindingListener
监听对象绑定到session上的事件
Interface HttpSessionActivationListener
(了解) 监听session序列化及反序列化的事件
三,程序实现
1.生命周期监听器
声明周期监听器: 监听对象的创建、销毁的过程!
监听器开发步骤:
1.写一个普通java类,实现相关接口;
2.配置(web.xml)
- 监听request对象的创建或者销毁
index.jsp
<%--
Created by IntelliJ IDEA.
User: 马欢欢
Date: 2017/5/30
Time: 10:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>分页查询</title>
</head>
<body>
<h2>Hello World!</h2>
<h1>欢迎访问index页面</h1>
<%
request.setAttribute("huan","mahuan");
%>
</body>
</html>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--监听request对象创建,销毁-->
<listener>
<listener-class>ed.huan.listener.RequestListener</listener-class>
</listener>
</web-app>
RequestListener.java
package ed.huan.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/**
* Created by 马欢欢 on 17-6-1.
*/
public class RequestListener implements ServletRequestListener {
//对象销毁
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
Object obj = servletRequestEvent.getServletRequest().getAttribute("huan");
System.out.println("2.对象销毁"+obj);
}
//对象创建
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
Object obj1 = servletRequestEvent.getServletRequest().getAttribute("huan");
System.out.println("1.对象创建"+ obj1);
}
}
2.属性监听
- 监听session属性的变化
index.jsp
<%--
Created by IntelliJ IDEA.
User: 马欢欢
Date: 2017/5/30
Time: 10:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>分页查询</title>
</head>
<body>
<h2>Hello World!</h2>
<h1>欢迎访问index页面</h1>
<%
session.setAttribute("huan","mahuan");
session.removeAttribute("huan");
%>
</body>
</html>
web.xml
<listener>
<listener-class>ed.huan.listener.Session</listener-class>
</listener>
SessionAttributeListener.java
package ed.huan.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/**
* Created by 马欢欢 on 17-6-1.
*/
public class RequestListener implements ServletRequestListener {
//对象销毁
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
Object obj = servletRequestEvent.getServletRequest().getAttribute("huan");
System.out.println("2.对象销毁"+obj);
}
//对象创建
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
Object obj1 = servletRequestEvent.getServletRequest().getAttribute("huan");
System.out.println("1.对象创建"+ obj1);
}
}
上一篇:过滤器 - 学习笔记