目录
一、监听器Listener
javaEE包括13门规范 在课程中主要学习 servlet技术 和 jsp技术
其中 servlet规范包括三个技术点:servlet listener filter
1.什么是监听器?
监听器就是监听某个对象的的状态变化的组件
监听器的相关概念:
- 事件源:被监听的对象 —– 三个域对象 request session servletContext
- 监听器:监听事件源对象 事件源对象的状态的变化都会触发监听器 —- 6+2
- 注册监听器:将监听器与事件源进行绑定
- 响应行为:监听器监听到事件源的状态变化时 所涉及的功能代码 —- 程序员编写代码
2.监听器有哪些?
第一维度:按照被监听的对象划分:ServletRequest域 HttpSession域 ServletContext域
第二维度:监听的内容分:监听域对象的创建与销毁的 监听域对象的属性变化的
3.监听三大域对象的创建与销毁的监听器
(1)监听ServletContext域的创建与销毁的监听器ServletContextListener
1)Servlet域的生命周期
- 何时创建:服务器启动创建
- 何时销毁:服务器关闭销毁
2)监听器的编写步骤(重点):
- a、编写一个监听器类去实现监听器接口
- b、覆盖监听器的方法
- c、需要在web.xml中进行配置—注册
4)配置文件:
5)ServletContextListener监听器的主要作用
- a、初始化的工作:初始化对象 初始化数据 —- 加载数据库驱动 连接池的初始化
- b、加载一些初始化的配置文件 — spring的配置文件
- c、任务调度—-定时器—-Timer/TimerTask
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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; public class MyServletContextListener implements ServletContextListener{ @Override //监听context域对象的创建 public void contextInitialized(ServletContextEvent sce) { //就是被监听的对象---ServletContext //ServletContext servletContext = sce.getServletContext(); //getSource就是被监听的对象 是通用的方法 //ServletContext source = (ServletContext) sce.getSource(); //System.out.println("context创建了...."); //开启一个计息任务调度----每天晚上12点 计息一次 //Timer timer = new Timer(); //task:任务 firstTime:第一次执行时间 period:间隔执行时间 //timer.scheduleAtFixedRate(task, firstTime, period); /*timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println("银行计息了....."); } } , new Date(), 5000);*/ //修改成银行真实计息业务 //1、起始时间: 定义成晚上12点 //2、间隔时间:24小时 /*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //String currentTime = "2016-08-19 00:00:00"; String currentTime = "2016-08-18 09:34: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);*/ } //监听context域对象的销毁 @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("context销毁了...."); } } |
(2)监听Httpsession域的创建于销毁的监听器HttpSessionListener
1)HttpSession对象的生命周期
- 何时创建:第一次调用request.getSession时创建
- 何时销毁:服务器关闭销毁 session过期 手动销毁
2)HttpSessionListener的方法
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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销毁"); } } |
(3)监听ServletRequest域创建与销毁的监听器ServletRequestListener
1)ServletRequest的生命周期
- 创建:每一次请求都会创建request
- 销毁:请求结束
4.监听三大域对象的属性变化的
(1)域对象的通用的方法:
setAttribute(name,value)
— 触发添加属性的监听器的方法
— 触发修改属性的监听器的方法
getAttribute(name)
removeAttribute(name)
— 触发删除属性的监听器的方法
(2)ServletContextAttibuteListener监听器
(3)HttpSessionAttributeListener监听器(同上)
(4) ServletRequestAriibuteListenr监听器(同上)
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.itheima.attribute; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; public class MyServletContextAttributeListener implements ServletContextAttributeListener{ @Override public void attributeAdded(ServletContextAttributeEvent scab) { //放到域中的属性 System.out.println(scab.getName());//放到域中的name System.out.println(scab.getValue());//放到域中的value } @Override public void attributeRemoved(ServletContextAttributeEvent scab) { System.out.println(scab.getName());//删除的域中的name System.out.println(scab.getValue());//删除的域中的value } @Override public void attributeReplaced(ServletContextAttributeEvent scab) { System.out.println(scab.getName());//获得修改前的name System.out.println(scab.getValue());//获得修改前的value } } |
5.与session中的绑定的对象相关的监听器(对象感知监听器)
(1)即将要被绑定到session中的对象有几种状态
- 绑定状态:就一个对象被放到session域中
- 解绑状态:就是这个对象从session域中移除了
- 钝化状态:是将session内存中的对象持久化(序列化)到磁盘
- 活化状态:就是将磁盘上的对象再次恢复到session内存中
面试题:当用户很对时,怎样对服务器进行优化?
(2)绑定与解绑的监听器HttpSessionBindingListener
(3)钝化与活化的监听器HttpSessionActivationListener
可以通过配置文件 指定对象钝化时间 — 对象多长时间不用被钝化
在META-INF下创建一个context.xml
1 2 3 4 5 6 7 |
<Context> <!-- maxIdleSwap:session中的对象多长时间不使用就钝化 --> <!-- directory:钝化后的对象的文件写到磁盘的哪个目录下 配置钝化的对象文件在 work/catalina/localhost/钝化文件 --> <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1"> <Store className="org.apache.catalina.session.FileStore" directory="itcast205" /> </Manager> </Context> |
被钝化到work/catalina/localhost/的文件