JavaWeb(36) : 监听器Listener(一)

一、监听器介绍

1.1、监听器的概念

监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动。监听器其实就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法立即被执行。

 

1.2、监听窗口行为案例

package waf.yty.demo1;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Demo1 {
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setSize(300, 200);
		frame.setLocation(300, 300);
		frame.setLayout(new FlowLayout());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JButton btn = new JButton("确定");
		frame.add(btn);
		btn.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				System.out.println("hello~");
				
			}
		});
		
		frame.setVisible(true);
	}

}

 

二、JavaWeb中的监听器

2.1、基本概念

JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext, HttpSession和 ServletRequest等域对象的创建与销毁事件,以及监听这些域对象中的属性发生修改的事件。

2.2、Servlet监听器的分类

在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为ServletContextHttpSessionServletRequest这三个域对象。

Servlet规范针对这三个对象上的操作,又把多种类型的监听器划分为三种类型:

  • 监听域对象自身的创建和销毁的事件监听器
  • 监听域对象中的属性的增加和删除的事件监听器
  • 监听绑定到HttpSession域中的某个对象的状态的事件监听器

2.3、监听ServletContext域对象的创建和销毁

ServletContextListener接口用于监听ServletContext对象的创建和销毁事件。实现了ServletContextListener接口的类都可以对ServletContext对象的创建和销毁进行监听。

当ServletContext对象被创建时,激发contextInitialized (ServletContextEvent sce)方法。

当ServletContext对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法。

ServletContext域对象创建和销毁时机:

创建:服务器启动针对每一个Web应用创建ServletContext

销毁:服务器关闭前先关闭代表每一个web应用的ServletContext

编写一个MyServletContextListener类,实现ServletContextListener接口,监听ServletContext对象的创建和销毁 :

1、编写监听器,代码如下:

package waf.yty.web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
 * ServletContext生死监听
 * @author yangtengyu
 *
 * 可以在这个监听器存放一些tomcat启动时就要完成的代码
 */
public class AListener implements ServletContextListener {

	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("我来也!");
		
	}
	
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("我去也!");

	}


}

2、在web.xml文件中注册监听器

我们在上面的中讲到,要想监听事件源,那么必须将监听器注册到事件源上才能够实现对事件源的行为动作进行监听,在JavaWeb中,监听的注册是在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>day20_1</display-name>
  <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>
  
  <listener>
  	<description>ServletContextListener监听器</description>
  	<listener-class>waf.yty.web.listener.AListener</listener-class>
  </listener>
  
</web-app>

经过这两个步骤,我们就完成了监听器的编写和注册,Web服务器在启动时,就会自动把在web.xml中配置的监听器注册到ServletContext对象上,这样开发好的MyServletContextListener监听器就可以对ServletContext对象进行监听了。

2.4、监听HttpSession域对象的创建和销毁

HttpSessionListener 接口用于监听HttpSession对象的创建和销毁

创建一个Session时,激发sessionCreated (HttpSessionEvent se) 方法

销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se) 方法

编写一个MyHttpSessionListener类,实现HttpSessionListener接口,监听HttpSession对象的创建和销毁 :

1、编写监听器,代码如下:

package waf.yty.web.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyHttpSessionListener implements HttpSessionListener {

	public void sessionCreated(HttpSessionEvent se) {
		System.out.println(se.getSession() + "创建了!");

	}

	public void sessionDestroyed(HttpSessionEvent se) {
		System.out.println("session销毁了!");

	}

}

2、在web.xml文件中注册监听器

  <listener>
  	<description>HttpSessionListener监听器</description>
  	<listener-class>waf.yty.web.listener.MyHttpSessionListener</listener-class>
  </listener>
  
  <session-config>
  <!-- HttpSession1分钟后销毁 -->
  	<session-timeout>1</session-timeout>
  </session-config>

当我们访问jsp页面时,HttpSession对象就会创建,此时就可以在HttpSessionListener观察到HttpSession对象的创建过程了,我们可以写一个jsp页面观察HttpSession对象创建的过程。

如下:index.jsp

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>HttpSessionListener监听Session的创建</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
      一访问JSP页面,HttpSession就创建了,创建好的Session的Id是:${pageContext.session.id }
  </body>
</html>

运行结果如下:

控制台输出:

org.apache.catalina.session.StandardSessionFacade@1eb93b68创建了!
创建好的Session的id是:842C0C67BD1181740C9BFAA4B4C2BC9C
session销毁了!
session销毁了!

浏览器显示:

 一访问JSP页面,HttpSession就创建了,创建好的Session的Id是:842C0C67BD1181740C9BFAA4B4C2BC9C

2.5、监听ServletRequest域对象的创建和销毁

ServletRequestListener接口用于监听ServletRequest 对象的创建和销毁

Request对象被创建时,监听器的requestInitialized(ServletRequestEvent sre)方法将会被调用

Request对象被销毁时,监听器的requestDestroyed(ServletRequestEvent sre)方法将会被调用

ServletRequest域对象创建和销毁时机:

创建:用户每一次访问都会创建request对象

销毁:当前访问结束,request对象就会销毁

编写一个MyServletRequestListener类,实现ServletRequestListener接口,监听ServletRequest对象的创建和销毁 :

1、编写监听器,代码如下:

package waf.yty.web.listener;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class MyServletRequestListener implements ServletRequestListener {

	public void requestDestroyed(ServletRequestEvent sre) {
		
		System.out.println(sre.getServletRequest() + "销毁了!");
	}

	public void requestInitialized(ServletRequestEvent sre) {
		
		System.out.println(sre.getServletRequest() + "创建了!");
	}

}

2、在web.xml文件中注册监听器

<listener>
  	<description>ServletRequestListener监听器</description>
  	<listener-class>waf.yty.web.listener.MyServletRequestListener</listener-class>
  </listener>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值