一 监听器作用
主要是用来监听特定对象的创建对象或销毁,属性的变化的!是一个实现特定接口的普通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 生命周期监听
index.jsp
<%--
Created by IntelliJ IDEA.
User: pc
Date: 2017/9/6
Time: 17:48
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>欢迎访问index页面</h2>
<%
request.setAttribute("name","Yang");
%>
</body>
</html>
RequestListener.java
package entity;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/**
* Created by pc on 2017/9/12.
*/
public class RequestListener implements ServletRequestListener {
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
Object object = servletRequestEvent.getServletRequest().getAttribute("name");
System.out.println("2.对象销毁"+object);
}
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
Object object1 = servletRequestEvent.getServletRequest().getAttribute("name");
System.out.println("1.对象创建"+object1);
}
}
.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>
<listener>
<listener-class>entity.RequestListener</listener-class>
</listener>
</web-app>
2 属性监听(监听session变化)
index.jsp
<%--
Created by IntelliJ IDEA.
User: pc
Date: 2017/9/6
Time: 17:48
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>欢迎访问index页面</h2>
<%
session.setAttribute("name","YY");
session.removeAttribute("name");
%>
</body>
</html>
SessionAttributeListener.java
package entity;
import javax.servlet.http.*;
/**
* Created by pc on 2017/9/12.
*/
public class SessionAttributeListener implements HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("创建session");
}
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("移除session");
}
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("修改session");
}
}
.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>
<listener>
<listener-class>entity.SessionAttributeListener</listener-class>
</listener>
</web-app>