(22)监听器

一 监听器

javaEE包括13种规范,我们主要学习servlet技术和jsp技术

其中servlet规范包括三个技术点:servlet, listener,filter

1 什么是监听器

监听器就是监听某个对象的状态变化组件

监听器从两个维度分为6种(共8种)

第一维度:从被监听的对象来说,ServletContext(web应用上下文)对象,HttpSession对象,ServletRequest对象

第二维度:监听的内容分为:监听域对象的创建与销毁  监听域对象的属性变化

(1)ServletContext
package com.itheima.create;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
 * servletcontext   为web应用的上下文域对象
 * @author Wl
 *
 */
public class MyServletContextListener implements ServletContextListener{

	@Override
	//监听servletcontext对象的创建
	public void contextInitialized(ServletContextEvent sce) {
		//下面一句代码就是被监听的对象---------ServletContext
		//ServletContext servletContext = sce.getServletContext();
		//也是获得被监听的方法  是通用的方法
		//Object source = sce.getSource();
		//System.out.println("ServletContext创建了");
		
		
		//开启一个计息的任务调度------每天晚上12点计息一次
		Timer timer = new Timer();
		//task任务firsttime第一次执行时间period间隔执行时间
		//timer.scheduleAtFixedRate(task, firstTime, period);
		
		
		
		//现在我们要修改成银行真正的计息业务
		//1.起始时间: 定义成晚上12点
		//2.间隔时间: 每隔24小时
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String currentTime = "2016-08-08 00:00:00";
		Date parse = null;
		try {
			parse = format.parse(currentTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				System.out.println("银行计息了..........");
			}
		}, parse, 24*60*60*1000);
		
		
		
		
		
	}
	@Override
	//监听servletcontext对象的销毁
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("ServletContext销毁了");
	}
}

注册监听器

<listener>
    <listener-class>com.itheima.create.MyServletContextListener</listener-class>
  </listener>
package com.itheima.attribute;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
/**
 * 这里做一下注释,这里的代码是监听器代码(web应用上下文属性监听)
 * 另外相同的包中测试的代码servlet中向servletcontext域中设置
 * 了名字为name值为tom的属性,监听到了就向控制台输出。
 * @author Wl
 *
 */
public class MyServletContextAttributeListener implements ServletContextAttributeListener{

	@Override
	public void attributeAdded(ServletContextAttributeEvent arg0) {
		//放到域中的属性
		System.out.println(arg0.getName());//放到域中的name
		System.out.println(arg0.getValue());//放到域中的value
		
	}

	@Override
	public void attributeRemoved(ServletContextAttributeEvent arg0) {
		System.out.println(arg0.getName());//删除的域中的name
		System.out.println(arg0.getValue());//删除的域中的value
		
	}

	@Override
	public void attributeReplaced(ServletContextAttributeEvent arg0) {
		System.out.println(arg0.getName());//获得修改前的name
		System.out.println(arg0.getValue());//获得修改前的value
		
	}

}
package com.itheima.attribute;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestMyServletContextAttributeListener extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ServletContext context = this.getServletContext();//web应用上下文
		//向context域中存储数据
		context.setAttribute("name", "tom");
		//改context域中的数据
		context.setAttribute("name", "lucy");
		//删除context数据
		context.removeAttribute("name");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
<listener>
    <listener-class>com.itheima.attribute.MyServletContextAttributeListener</listener-class>
  </listener>
(2)HttpSession
package com.itheima.create;

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

public class MyHttpSessionListener implements HttpSessionListener{

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		System.out.println("session创建"+se.getSession().getId());
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		System.out.println("session销毁");
		
	}

}

注册监听器

<listener>
    <listener-class>com.itheima.create.MyHttpSessionListener</listener-class>
  </listener>
(3)ServletRequest

与上面两个对象基本相同

二 另外两种监听器(无需注册,直接绑定对象)

HttpSessionBindingListener和HttpSessionActivationListener,这两种监听器与session有关,前者监听session中是否绑定(解绑)对象,后者监听session中钝化(活化)对象,钝化指的是将session(服务器端)信息存在客户端磁盘,活化指的是从本地磁盘加载到内存中

(1)HttpSessionBindingListener
package com.itheima.domain;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/**
 * 这里说明一下总共8种监听器,其实就是6+2
 * 前面6种为2*3,一个初始化,一个改变属性,
 * 分别对应三个域对象
 * 下面的代码是一种叫做感知监听器的东西,对应2个方法
 * 一个绑定,一个解绑,并且这种感知监听器直接绑定在对象身上
 * 无需注册,并且这两个监听器与session相关
 * @author Wl
 *
 */
public class Person implements HttpSessionBindingListener{
	private String id;
	private String name;
	public String getId() {
		return id;
	}
	public void setId(String 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("person被绑定了");
		
		
	}
	@Override
	//解绑的方法
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		System.out.println("person被解绑了");
		
	}
	
}
package com.itheima.domain;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TestPersonBindingServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		//将person对象绑定到session域中
		Person p = new Person();
		p.setId("100");
		p.setName("zhangsanfeng");
		
		session.setAttribute("person", p);
		
		//将person从session域中解绑
		session.removeAttribute("person");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

(2)HttpSessionActivationListener
package com.itheima.domain;

import java.io.Serializable;

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
/**
 * 为customer绑定一个钝化活化监听器
 * @author Wl
 *
 */
public class Customer implements HttpSessionActivationListener,Serializable{
	private String id;
	private String name;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	//活化
	@Override
	public void sessionDidActivate(HttpSessionEvent arg0) {
		System.out.println("customer被活化");
		
	}
	//钝化
	@Override
	public void sessionWillPassivate(HttpSessionEvent arg0) {
		System.out.println("customer被钝化");
		
	}
	
}
package com.itheima.domain;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 这里我解释一下钝化和活化的概念
 * 钝化:指将session中的数据存放在硬盘中
 * 活化:将硬盘中用户的数据取回到内存中
 * @author Wl
 *
 */
public class TestCustomerActiveServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		//将customer放入session域中
		Customer customer = new Customer();
		customer.setId("200");
		customer.setName("lucy");
		session.setAttribute("customer", customer);
		System.out.println("customer被放在session域中了");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

这里做一下总结:其实监听器就是监听程序中的变化,监听到了之后我们就可以对其进行一些操作。







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值