1.Listener监听器——Servlet事件监听器概述
1.监听域对象的生命周期
1.创建监听器
在chapter08项目中创建一个cn.itcst.chapter08.listener包,在该包中编写一个MyListener类,这个类实现了ServletContextListener, HttpSessionListener, ServletRequestListener,这3个监听器接口,并实现这些接口中的所有方法。
创建listener类方法如下:
MyListener类中的代码如下:
package cn.itcast.chapter08.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class MyListener implements ServletContextListener, HttpSessionListener, ServletRequestListener {
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("HttpSession对象被创建了");
}
public void requestDestroyed(ServletRequestEvent arg0) {
System.out.println("ServletRequest对象被销毁了");
}
public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("ServletRequest对象被创建了");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("HttpSession对象被销毁了");
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContext对象被销毁了");
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContext对象被创建了");
}
}
2.添加监听器类信息
在chapter08项目下的web.xml文件中,添加MyListener事件监听器的信息,代码如下:
<listener>
<listener-class>
cn.itcast.chapter08.listener.MyListener
</listener-class>
</listener>
3.启动项目
查看ServletContext对象创建信息,如图:
4.关闭项目
查看ServletContext对象销毁信息,如图:
5.创建测试页面
在chapter08项目的WebContent目录中编写一个简单的页面文件myjsp.jsp,如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>this is MyJsp.jsp</title>
</head>
<body>
监听器正在监听
</body>
</html>
6.设置监听超时信息
在chapter08的web.xml文件中设置session的超时时间为2min,代码如下:
<session-config>
<session-timeout>2</session-timeout>
</session-config>
7.重启Tomcat服务器
在地址栏输入“http://localhost:2222/chapter08/MyListener”,访问myjsp.jsp页面,控制台窗口效果如图:
关闭myjsp.jsp页面或者保持页面不刷新,等待2min左右,HttpSession对象将会被销毁,如下:
2.监听域对象的属性变更
1.创建测试页面
在chapter08项目的WebContext的根目录中,编写一个testattribute.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>
<h3>这是一个测试对象属性信息监听器的页面</h3>
<%
getServletContext().setAttribute("username", "itcast");
getServletContext().setAttribute("username", "itheima");
getServletContext().removeAttribute("username");
session.setAttribute("username", "itcast");
session.setAttribute("username", "itheima");
session.removeAttribute("username");
request.setAttribute("username", "itcast");
request.setAttribute("username", "itheima");
request.removeAttribute("username");
%>
</body>
</html>
2.创建监听器
在chapter08项目的cn.itcst.chapter08.listener包中,编写一个名称为MyAttributeListener的监听器类,它实现了ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener接口,实现这些接口中的所有方法,如下:
package cn.itcast.chapter08.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class MyAttributeListener implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {
public void attributeAdded(ServletContextAttributeEvent sae) {
String name = sae.getName();
System.out.println("ServletContext添加属性:" + name + "=" + sae.getServletContext().getAttribute(name));
}
public void attributeRemoved(ServletContextAttributeEvent sae) {
String name = sae.getName();
System.out.println("ServletContext移除属性:" + name);
}
public void attributeReplaced(ServletContextAttributeEvent sae) {
String name = sae.getName();
System.out.println("ServletContext替换属性:" + name + "=" + sae.getServletContext().getAttribute(name));
}
public void attributeRemoved(ServletRequestAttributeEvent sra) {
String name = sra.getName();
System.out.println("ServletRequest添加属性:" + name + "=" + sra.getServletRequest().getAttribute(name));
}
public void attributeAdded(ServletRequestAttributeEvent sra) {
String name = sra.getName();
System.out.println("ServletRequest移除属性:" + name);
}
public void attributeReplaced(ServletRequestAttributeEvent sra) {
String name = sra.getName();
System.out.println("ServletRequest替换属性:" + name + "=" + sra.getServletRequest().getAttribute(name));
}
public void attributeAdded(HttpSessionBindingEvent hbe) {
String name = hbe.getName();
System.out.println("HttpSession添加属性:" + name + "=" + hbe.getSession().getAttribute(name));
}
public void attributeRemoved(HttpSessionBindingEvent hbe) {
String name = hbe.getName();
System.out.println("HttpSession移除属性:" + name);
}
public void attributeReplaced(HttpSessionBindingEvent hbe) {
String name = hbe.getName();
System.out.println("HttpSession替换属性:" + name + "=" + hbe.getSession().getAttribute(name));
}
}
3.添加监听信息
在web.xml文件中,添加MyAttributeListener事件监听器信息,如下:
<listener>
<listener-class>
cn.itcast.chapter08.listener.MyAttributeListener
</listener-class>
</listener>
4.启动项目,查看结果
在地址栏输入“http://localhost:2222/chapter08/testattribute.jsp”,访问testattribute.jsp页面,控制台显示信息如下: