目录
一、Listener介绍
监听器介绍
什么是监听器
Javaweb中的监听器是用于监听web常见对象HttpServletRequest,HttpSession,ServletContext
监听器的作用
1.监听web对象创建与销毁.
2.监听web对象的属性变化
3.监听session绑定javaBean操作.
监听机制相关概念
1.事件----一件事情
2.事件源—产生这件事情的源头
3.注册监听—将监听器与事件绑定,当事件产生时,监听器可以知道,并进行处理。
4.监听器—对某件事情进行处理监听的一个对象
web监听器介绍
1.javaweb监听器介绍
1.监听web对象创建与销毁的监听器
1.ServletContextListener
2.HttpSessionListener
3.ServletRequestListener
2.监听web对象属性变化
1.ServletContextAttributeListener
2.HttpSessionAttributeListener
3.ServletRequestAttributeListener
3.监听session绑定javaBean
1.HttpSessionBindingListener
2.HttpSessionActivationListener
2.javaweb监听器创建步骤
1.创建一个类,实现指定的监听器接口
2.重写接口中的方法.
3.在web.xml文件中配置监听
3.演示监听对象创建与销毁
1.ServletContext对象的创建与销毁监听
ServletContext对象的创建与销毁分析:ServletContext对象是服务器开启时创建。服务器关闭时销毁。
2.HttpSession对象的创建与销毁监听
1.HttpSession对象的创建与销毁分析:
1.session对象创建:取决于请求中是否有jsessinid,如果有,可能会获取一个已经存在的session对象。如果没有,会创建一个新的session对象.
2.销毁session:
1.默认超时 30分钟
2.关闭服务器
3.invalidate()方法
4.setMaxInactiveInterval(int interval) 可以设置超时时间
4.HttpServletRequest对象的创建与销毁监听
1.HttpServletRequest对象的创建与销毁分析:request对象是发送请求时创建,当响应产生时,销毁.
5.监听步骤:
1.创建一个类,实现指定的监听器接口
2.重写接口中的方法
3.在web.xml文件中对监听器进行注册。
二、监听域对象的生命周期
要想对Servlet域对象的生命周期进行监听,需要实现域对应的3.ServletContextListener,HttpSessionLister和ServletRequestListener接口
实现步骤
1.创建监听器,实现监听器接口
package cn.itcast.chapter08.listener;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyListener implements
ServletContextListener, HttpSessionListener,ServletRequestListener {
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContext对象被创建了");
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContext对象被销毁了");
}
public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("ServletRequest对象被创建了");
}
public void requestDestroyed(ServletRequestEvent arg0) {
System.out.println("ServletRequest对象被销毁了");
}
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("HttpSession对象被创建了");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("HttpSession对象被销毁了");
}
}
2.添加监听器类信息
一个完整的Servlet事件监听器包括Listener类和<listener>配置信息,而一个web.xml中可以配置多个监听器。同一种类型的监听器也可以配置多个,触发的时候服务器会顺序执行各个监听器的相应方法。
3.启动项目,查看servletContext对象创建信息
4.关闭项目,查看ServletContext对象销毁信息
5.创建测试页面
查看HttpSessionListener和ServletRequestListener监听器的运行效果,创建myjsp.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>this is MyJsp.jsp page</title>
</head>
<body>
这是一个测试监听器的页面
</body>
</html>
6.设置监听超时信息
<session-timeout>标签指定的超时必须为一个整数。如果这个整数为0或负整数,则session永远不会超时;如果这个数是正整数,则项目中的session将在指定分钟后超时。
7.重启项目,查看结果
zai
再次刷新
再次访问myjsp.jsp页面,在控制台窗口中会再次输出ServletRequest对象被创建与被销毁的信息,但不会创建新的用了HttpSession对象。这是因为Web容器会为每次访问请求都创建一个新的ServletRequest对象,而对于同一个浏览器在会话期间的后续访问是不会再创建新的HttpSession对象的。
HttpSession对象被销毁了,Web服务器调用了监听器对象的sessionDestroyed()方法。
三、 监听域对象的属性变更
实现步骤
1.创建测试页面
观察各个对象域对象属性性监听器的作用
2.创建监听器
实现接口所有方法
package cn.itcast.chapter08.listener;
import javax.servlet.*;
import javax.servlet.http.*;
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 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));
}
public void attributeAdded(ServletRequestAttributeEvent sra) {
String name = sra.getName();
System.out.println("ServletRequest添加属性:" + name + "="
+ sra.getServletRequest().getAttribute(name));
}
public void attributeRemoved(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));
}
}
3.添加监听信息
4.启动项目,查看结果
可以看出,在ServletContext、HttpSession和ServletRequest 3个域对象中.
分别完成了增加、替换和删除username属性值的操作。
总结
1、简述过滤器的作用
Filter被称作过滤器或者拦截器,其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理前后实现一些特殊功能。
2、简述Servlet事件监听器的作用
1)监听Web应用程序中ServletContext、HttpSession和ServletRequest等域对象的创建 和销毁过程。
2)监听ServletContext、HttpSession和ServletRequest等域对象属性的修改。
3)感知绑定到HttpSession域中某个对象的状态。