一、监听器
1.1. 监听器的作用
+++ 什么是监听器?
监听器: 主要是用来监听特定对象的创建或销毁、属性的变化的!
是一个实现特定接口的普通java类!
使用监听器可以实现在线列表显示、踢人、统计当前登录人数等功能
+++ 什么对象需要被监听?
对象:
自己创建自己用 (不用监听)
别人创建自己用 (需要监听)
Servlet中哪些对象需要监听?
request / session / servletContext
分别对应的是request监听器、session相关监听器、servletContext监听器
在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为
ServletContext, HttpSession 和 ServletRequest 这三个域对象。
1.2 监听器的分类
监听器接口(八个接口):
一、生命周期监听器(监听对象创建/销毁的监听器接口)
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序列化及反序列化的事件
404(路径写错)
500(服务器错误,调试)
1.2生命周期监听器
声明周期监听器: 监听对象的创建、销毁的过程!
监听器开发步骤:
1.写一个普通java类,实现相关接口;
2.配置(web.xml)
二、生命周期监听器
2.1 ServletRequestListener(request对象监听器)
|-- ServletRequestListener:
requestInitialized(ServletRequestEvent arg0)
request对象创建时,调用该方法。
requestDestroyed(ServletRequestEvent arg0)
request对象销毁时,调用该方法。
1.ServletRequestListener是request对象监听器。监听request对象的创建和销毁。
2.执行时间:
用户发送一次http请求,
request对象就会被创建,触发监听器的requestInitialized()
服务端响应数据后,
request对象就被销毁,触发监听器的requestDestroyed()
MyServletReqestListener.java
/**
* 监听request对象的创建、销毁
*
* @author BGS
*
*/
public class MyServletReqestListener implements ServletRequestListener {
/**
* request对象销毁时调用
*/
@Override
public void requestDestroyed(ServletRequestEvent sr) {
String msg=(String)sr.getServletRequest().getAttribute("cn");
System.out.println(msg);
System.out.println("MyServletReqestListener--requestDestroyed");
}
/**
* http对象创建时调用
*/
@Override
public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("MyServletReqestListener--requestInitialized");
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%request.setAttribute("cn", "china"); %>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 监听request对象的创建、销毁 -->
<listener>
<listener-class>org.jsoft.filter.MyServletReqestListener</listener-class>
</listener>
</web-app>
访问index.jsp。
监听器监听到request对象的创建。触发requestInitialized方法。
2.2 ServletContextListener(servletContext对象监听器)
|-- ServletContextListener:
contextInitialized(ServletContextEvent sce)
servletContext对象创建时,调用该方法。
contextDestroyed(ServletContextEvent sce)
servletContext对象销毁时,调用该方法。
1.ServletContextListener是servletContext对象监听器。监听servletContext对象的创建和销毁。
2.执行时间:
用户启动项目时,
ServletContext对象就会被创建,触发监听器的contextInitialized()
用户关闭项目时,
ServletContext对象就被销毁,触发监听器的contextDestroyed()
MyServletContextListener .java
/**
* 监听servletContext对象的创建和销毁
*
* @author BGS
*
*/
public class MyServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
String str=(String) sce.getServletContext().getAttribute("cn");
System.out.println(str);
System.out.println("MyServletContextListener--contextInitialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("MyServletContextListener--contextDestroyed");
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%application.setAttribute("cn", "china"); %>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 监听request对象的创建、销毁 -->
<listener>
<listener-class>org.jsoft.filter.MyServletReqestListener</listener-class>
</listener>
<!-- 监听servletContext对象的创建、销毁 -->
<listener>
<listener-class>org.jsoft.filter.MyServletContextListener</listener-class>
</listener>
</web-app>
启动tomcat服务器。
启动tomcat服务器,ServletContext对象就会被创建,此时触发监听器的开始方法。
关闭tomcat服务器,ServletContext对象就会被销毁,此时触发监听器的结束方法。
2.3 HttpSessionListener(HttpSession对象监听器)
|-- HttpSessionListener:
sessionCreated(HttpSessionEvent arg0)
HttpSession对象创建时,调用该方法。
sessionDestroyed(HttpSessionEvent arg0)
HttpSession对象销毁时,调用该方法。
1.HttpSessionListener是HttpSession对象监听器。监听HttpSession对象的创建和销毁。
2.执行时间:
1)jsp文件含有九大内置对象,会自动创建session。
用户请求jsp文件时,jsp文件就会自动创建对象,sesison对象也会被创建。
此时就会触发session对象监听器。
2)用户创建session时就会触发session对象监听器。
MyHttpSessionListener.java
/**
* 监听HttpSession对象的创建和销毁
*
* @author BGS
*
*/
public class MyHttpSessionListener implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent arg0) {
HttpSession session = arg0.getSession();
System.out.println("MyHttpSessionListener--sessionCreated");
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("MyHttpSessionListener--sessionDestroyed");
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%application.setAttribute("cn", "china"); %>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 监听request对象的创建、销毁 -->
<listener>
<listener-class>org.jsoft.filter.MyServletReqestListener</listener-class>
</listener>
<!-- 监听servletContext对象的创建、销毁 -->
<listener>
<listener-class>org.jsoft.filter.MyServletContextListener</listener-class>
</listener>
<!-- 监听HttpSession对象的创建、销毁 -->
<listener>
<listener-class>org.jsoft.filter.MyHttpSessionListener</listener-class>
</listener>
</web-app>
用户访问index.jjsp。jsp就会自动创建内置对象,包括session对象。
此时,就会触发session对象监听器。
三、属性周期监听器
3.1 ServletRequestAttributeListener(监听request对象属性的变化)
|-- ServletRequestAttributeListener:
attributeAdded(ServletRequestAttributeEvent srae)
添加属性时触发(可获取添加的属性值)
attributeRemoved(ServletRequestAttributeEvent srae)
移除属性时触发(可获取移除的属性值)
attributeReplaced(ServletRequestAttributeEvent srae)
替换属性时触发(可获取替换前的属性、替换后的属性值)
1.ServletRequestAttributeListener是request对象属性的监听器。监听request对象属性的变化。
/**
* 监听request对象属性的变化
*
* @author BGS
*
*/
public class MyServletReqestAttributeListener implements ServletRequestAttributeListener {
/**
* 新增属性时触发
*/
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
String str=(String) srae.getServletRequest().getAttribute("cn");
System.out.println("获取添加的属性:"+str);
}
/**
* 移除属性时触发
*/
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
String str=(String) srae.getValue();
System.out.println("获取移除的属性:"+str);
}
/**
* 替换属性时触发
*/
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
//获取替换前的属性值
Object value = srae.getValue();
System.out.println("获取替换前的属性:"+value);
//获取替换后的属性值
String str=(String) srae.getServletRequest().getAttribute("cn");
System.out.println("获取替换后的属性:"+str);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 监听request对象属性的变化 -->
<listener>
<listener-class>org.jsoft.filter.MyServletReqestAttributeListener</listener-class>
</listener>
</web-app>
3.2 ServletContextAttributeListener(监听servletContext对象的属性的变化)
|-- ServletContextAttributeListener:
attributeAdded(ServletContextAttributeEvent arg0)
添加属性时触发(可获取添加的属性值)
attributeRemoved(ServletContextAttributeEvent arg0)
移除属性时触发(可获取移除的属性值)
attributeReplaced(ServletContextAttributeEvent arg0)
替换属性时触发(可获取替换前的属性、替换后的属性值)
1.ServletContextAttributeListener是servletContext对象属性的监听器。监听servletContext对象属性的变化。
/**
* 监听servletContext属性的变化
*
* @author BGS
*
*/
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
//新增属性时触发
@Override
public void attributeAdded(ServletContextAttributeEvent arg0) {
Object str = arg0.getServletContext().getAttribute("cn");
System.out.println("获取新增的属性:"+str);
}
//移除属性时触发
@Override
public void attributeRemoved(ServletContextAttributeEvent arg0) {
Object str = arg0.getValue();
System.out.println("获取移除的属性:"+str);
}
//替换属性时触发
@Override
public void attributeReplaced(ServletContextAttributeEvent arg0) {
//获取替换前的属性
Object value = arg0.getValue();
System.out.println("获取替换前的属性:"+value);
//获取替换后的属性
Object str = arg0.getServletContext().getAttribute("cn");
System.out.println("获取替换后的属性:"+str);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 监听request对象属性的变化 -->
<listener>
<listener-class>org.jsoft.filter.MyServletReqestAttributeListener</listener-class>
</listener>
<!-- 监听servletContext对象属性的变化 -->
<listener>
<listener-class>org.jsoft.filter.MyServletContextAttributeListener</listener-class>
</listener>
</web-app>
index.xml
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%application.setAttribute("cn", "china");
application.setAttribute("cn", "china1");
application.removeAttribute("cn");
%>
</body>
</html>
3.3 HttpSessionAttributeListener(监听session对象的属性的变化)
|-- HttpSessionAttributeListener:
attributeAdded(ServletContextAttributeEvent arg0)
添加属性时触发(可获取添加的属性值)
attributeRemoved(ServletContextAttributeEvent arg0)
移除属性时触发(可获取移除的属性值)
attributeReplaced(ServletContextAttributeEvent arg0)
替换属性时触发(可获取替换前的属性、替换后的属性值)
1.HttpSessionAttributeListener是session对象属性的监听器。监听session对象属性的变化。
/**
* 监听session对象属性的变化
*
* @author BGS
*
*/
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener{
//新增属性时触发
@Override
public void attributeAdded(HttpSessionBindingEvent arg0) {
Object attribute = arg0.getSession().getAttribute("cn");
System.out.println("获取新增的属性:"+attribute);
}
//移除属性时触发
@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
Object attribute = arg0.getValue();
System.out.println("获取移除的属性:"+attribute);
}
//替换属性时触发
@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
Object attribute = arg0.getValue();
System.out.println("获取替换前的属性:"+attribute);
Object attribute2 = arg0.getSession().getAttribute("cn");
System.out.println("获取替换后属性:"+attribute2);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 监听request对象属性的变化 -->
<listener>
<listener-class>org.jsoft.filter.MyServletReqestAttributeListener</listener-class>
</listener>
<!-- 监听servletContext对象属性的变化 -->
<listener>
<listener-class>org.jsoft.filter.MyServletContextAttributeListener</listener-class>
</listener>
<!-- 监听HttpSession对象属性的变化 -->
<listener>
<listener-class>org.jsoft.filter.MyHttpSessionAttributeListener</listener-class>
</listener>
</web-app>
index.xml
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%application.setAttribute("cn", "china");
application.setAttribute("cn", "china1");
application.removeAttribute("cn");
%>
</body>
</html>
四、其他监听器(session相关监听器)
4.1 HttpSessionBindingListener
HttpSessionBindingListener :
监听对象绑定/解除绑定到sesison上的事件!
步骤:
实体对象实现接口; 再把对象放入session对象/移除,就会触发监听代码。
作用:
(上线提醒!)
区别:(与其他监听器的区别)
这个session监听器,和上面的声明周期、属性监听器区别?
-- 不用再web.xml配置
-- 因为监听的对象是自己创建的对象,不是服务器对象!
Admim.java
/**
* 绑定session的实体对象
*
* @author BGS
*
*/
public class Admin implements HttpSessionBindingListener{
private int id ;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void valueBound(HttpSessionBindingEvent arg0) {
System.out.println("该对象放入到session中");
}
@Override
public void valueUnbound(HttpSessionBindingEvent arg0) {
System.out.println("该对象从session中移除");
}
}
index.jsp
<%@page import="org.jsoft.filter.Admin"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
//放入对象到session中
session.setAttribute("cn", new Admin());
//移除对象
session.removeAttribute("cn");
%>
</body>
</html>
访问index.jsp。
admin是绑定sesison的实体对象。
首先把admin对象放入到session,则自动触发valueBound()
把admin对象从sesson中移除,则自动触发valueUnbound()