Listener监听器

Listener介绍:

1.监听器:

Javaweb中的监听器是用于监听web常见对象HttpServletRequest,HttpSession,ServletContext
监听器的作用
1.监听web对象创建与销毁.
2.监听web对象的属性变化
3.监听session绑定javaBean操作.

监听机制相关概念

1.事件----一件事情
2.事件源—产生这件事情的源头
3.注册监听—将监听器与事件绑定,当事件产生时,监听器可以知道,并进行处理。
4.监听器—对某件事情进行处理监听的一个对象

2.web监听器介绍:

javaweb监听器介绍

1.监听web对象创建与销毁的监听器
                1.ServletContextListener
                2.HttpSessionListener
                3.ServletRequestListene

2.监听web对象属性变化
                1.ServletContextAttributeListener
                2.HttpSessionAttributeListener
                3.ServletRequestAttributeListener

3.监听session绑定javaBean
                1.HttpSessionBindingListener
                2.HttpSessionActivationListene

javaweb监听器创建步骤

1.创建一个类,实现指定的监听器接口
 2.重写接口中的方法.
 3.在web.xml文件中配置监听 

演示监听对象创建与销毁 

1.ServletContext对象的创建与销毁监听
        ServletContext对象的创建与销毁分析:ServletContext对象是服务器开启时创建。服务器关闭时销毁。
        2.HttpSession对象的创建与销毁监听
                1.HttpSession对象的创建与销毁分析:
                        1.session对象创建:取决于请求中是否有jsessinid,如果有,可能会获取一个已经存在的session对象。如果没有,会创建一个新的session对象.
                        2.销毁session:
                        1.默认超时 30分钟
                        2.关闭服务器
                        3.invalidate()方法
                        4.setMaxInactiveInterval(int interval) 可以设置超时时间

HttpServletRequest对象的创建与销毁监听

HttpServletRequest对象的创建与销毁分析:request对象是发送请求时创建,当响应产生时,销毁 

监听步骤

1.创建一个类,实现指定的监听器接口
2.重写接口中的方法
3.在web.xml文件中对监听器进行注册。 

监听域对象的生命周期 

要想对Servlet域对象的生命周期进行监听,需要实现域对应的      3.ServletContextListener,HttpSessionLister和ServletRequestListener接口

实现步骤:

创建监听器,实现监听器接口

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对象被销毁了");
	}
}

添加监听器类信息

  一个完整的Servlet事件监听器包括Listener类和<listener>配置信息,而一个web.xml中可以配置多个监听器。同一种类型的监听器也可以配置多个,触发的时候服务器会顺序执行各个监听器的相应方法。 

启动项目,查看servletContext对象创建信息

关闭项目,查看ServletContext对象销毁信息 

 创建测试页面

<%@ 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>

设置监听超时信息

<session-timeout>标签指定的超时必须为一个整数。如果这个整数为0或负整数,则session永远不会超时;如果这个数是正整数,则项目中的session将在指定分钟后超时。

重启项目,查看结果

再次访问myjsp.jsp页面,在控制台窗口中会再次输出ServletRequest对象被创建与被销毁的信息,但不会创建新的用了HttpSession对象。这是因为Web容器会为每次访问请求都创建一个新的ServletRequest对象,而对于同一个浏览器在会话期间的后续访问是不会再创建新的HttpSession对象的。

  HttpSession对象被销毁了,Web服务器调用了监听器对象的sessionDestroyed()方法。

监听域对象的属性变更

创建测试页面

创建监听器


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));
	}
}

启动并查看结果

 

可以看出,在ServletContext、HttpSession和ServletRequest 3个域对象中.
分别完成了增加、替换和删除username属性值的操作。

1.Filter被称作过滤器或者拦截器,其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理前后实现一些特殊功能。

2、简述Servlet事件监听器的作用

     1.监听Web应用程序中ServletContext、HttpSession和ServletRequest等域对象的创建和销毁过程。

     2.监听ServletContext、HttpSession和ServletRequest等域对象属性的修改。

     3.感知绑定到HttpSession域中某个对象的状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值