监听器的使用
1.使用监听器的步骤
a.编写监听器类,实现要监听对象对应的接口
b.在web.xml中配置监听器
<listener>
<listener-class>com.xiaochen.listener.AListener01</listener-class>
</listener>
2.监听器的作用
i.监听request,session,application对象的创建和销毁
ii.监听request,session,application对象中属性的变更
应用实例1:创建两个jsp页面与监听器类,监听request,session,application对象的创建和销毁
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Alistener</display-name>
<listener>
<listener-class>com.xiaochen.listener.AListener02</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
AListener01.java
package com.xiaochen.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
//实现监听器接口
public class AListener01 implements ServletRequestListener,HttpSessionListener,ServletContextListener{
//request对象
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("监听ServletRequest对象,创建ServletRequest对象:"+sre);
}
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("监听ServletRequest对象,销毁ServletRequest对象:"+sre);
}
//session对象
public void sessionCreated(HttpSessionEvent se) {
System.out.println("监听HttpSession对象,创建HttpSession对象:"+se);
}
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("监听HttpSession对象,销毁HttpSession对象:"+se);
}
//application对象
public void contextInitialized(ServletContextEvent sce) {
System.out.println("监听ServletContext对象,创建ServletContext对象:"+sce);
}
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("监听ServletContext对象,销毁ServletContext对象:"+sce);
}
}
01.jsp
<%@ 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>
<a href="02.jsp">使session失效</a>
</body>
</html>
02.jsp
<%@ 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>
session销毁页面
<%
session.invalidate();
%>
</body>
</html>
运行结果图与原理图
应用实例2:创建一个jsp页面与监听器类,监听监听request,session,application对象中属性的变更
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Listener</display-name>
<listener>
<listener-class>com.xiaochen.listener.AListener02</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
AListener02.java
package com.xiaochen.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class AListener02 implements ServletRequestAttributeListener,HttpSessionAttributeListener,ServletContextAttributeListener{
//request对象
public void attributeAdded(ServletRequestAttributeEvent srae) {
String name=srae.getName();
Object value=srae.getValue();
System.out.println("ServletRequest【增加】属性:属性名:"+name+",属性值:"+value);
}
public void attributeReplaced(ServletRequestAttributeEvent srae) {
String name=srae.getName();
Object value=srae.getValue();
System.out.println("ServletRequest【修改】属性:属性名:"+name+",属性值:"+value);
}
public void attributeRemoved(ServletRequestAttributeEvent srae) {
String name=srae.getName();
Object value=srae.getValue();
System.out.println("ServletRequest【删除】属性:属性名:"+name+",属性值:"+value);
}
//session对象
public void attributeAdded(HttpSessionBindingEvent se) {
String name=se.getName();
Object value=se.getValue();
System.out.println("HttpSession【增加】属性:属性名:"+name+",属性值:"+value);
}
public void attributeReplaced(HttpSessionBindingEvent se) {
String name=se.getName();
Object value=se.getValue();
System.out.println("HttpSession【修改】属性:属性名:"+name+",属性值:"+value);
}
public void attributeRemoved(HttpSessionBindingEvent se) {
String name=se.getName();
Object value=se.getValue();
System.out.println("HttpSession【删除】属性:属性名:"+name+",属性值:"+value);
}
//application对象
public void attributeAdded(ServletContextAttributeEvent scae) {
String name=scae.getName();
Object value=scae.getValue();
System.out.println("ServletContext【增加】属性:属性名:"+name+",属性值:"+value);
}
public void attributeReplaced(ServletContextAttributeEvent scae) {
String name=scae.getName();
Object value=scae.getValue();
System.out.println("ServletContext【修改】属性:属性名:"+name+",属性值:"+value);
}
public void attributeRemoved(ServletContextAttributeEvent scae) {
String name=scae.getName();
Object value=scae.getValue();
System.out.println("ServletContext【删除】属性:属性名:"+name+",属性值:"+value);
}
}
listenerAttribute.jsp
<%@ 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>
<%
//request对象
request.setAttribute("name", "小白");
request.setAttribute("name", "小黑");
request.removeAttribute("name");
//session对象
session.setAttribute("age", 18);
session.setAttribute("age", 19);
session.removeAttribute("age");
//application对象
application.setAttribute("address", "龙游市");
application.setAttribute("address", "妖灵会馆");
application.removeAttribute("address");
%>
</body>
</html>
运行结果图