ServletListener对象学习笔记
JavaWeb学习笔记——监听器详解
知识概要:
1.监听器下例子举例
2.Servlet规范中的监听器
3.
4.
1. 监听器下例子举例说明:
|
/* Frame:事件源。发生事件的对象 WindowListener:监听器。(对应着事件源的某些内容) WindowEvent:事件对象。封装事件源 */ public class FrameDemo { public static void main(String[] args) { Frame f = new Frame("我的小窗"); f.setSize(400, 300); f.addWindowListener(new WindowAdapter() {//注册具体的监听器 public void windowClosing(WindowEvent e) { Frame f1 = (Frame) e.getSource(); f1.dispose(); } }); f.setVisible(true); } } 例子二: Student package com.itheima.base; public class Student { private String name; private StudentListener listener; public Student(String name){ this.name = name; } public void eat(){ if(listener!=null){ listener.preEat(new StudentEvent(this)); } System.out.println(name+":开吃"); } public void study(){ if(listener!=null){ listener.preStudy(new StudentEvent(this)); } System.out.println(name+":开始学习"); } public String getName() { return name; } public void addStudentListener(StudentListener listener){ this.listener = listener; } } |
|
|
StudentListener |
StudentEvent |
|
package com.itheima.base; public interface StudentListener { void preEat(StudentEvent e); void preStudy(StudentEvent e); } |
package com.itheima.base; public class StudentEvent { private Object source; public StudentEvent(Object source){ this.source = source; } public Object getSource(){ return source; } } |
|
studentDemo |
|
|
public class StudentDemo { public static void main(String[] args) { Student s = new Student("张三"); s.addStudentListener(new StudentListener() { public void preStudy(StudentEvent e) { Student s1 = (Student)e.getSource(); System.out.println(s1.getName()+",我给你一个香吻,你努力学习"); } public void preEat(StudentEvent e) { Student s1 = (Student)e.getSource(); System.out.println(s1.getName()+",我给你一个湿吻,润一下喉咙,好好吃饭"); } }); s.study(); s.eat(); } 打印结果: 张三,我给你一个香吻,你努力学习 张三:开始学习 张三,我给你一个湿吻,润一下喉咙,好好吃饭 张三:开吃 |
|
2.Servlet规范中的监听器
a、监听ServletContext、HttpSession、ServletRequest对象的创建和销毁的监听器。
ServletContextListener:两个方法
HttpSessionListener:两个方法
ServletRequestListener:两个方法
1.ServletContextListene监听器:
|
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /* 在web.xml中进行监听器注册 <listener> <listener-class>com.itheima.listener.MyServletContextListener</listener-class> </listener> */ public class MyServletContextListener implements ServletContextListener { //ServletContext对象创建时就会指定该方法 //ServletContext对象创建时就执行,执行一次:特点 //应用场景:完成系统的初始化。 //Spring框架:就是利用监听器完成Spring容器的初始化的 public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContext实例化了"); } //ServletContext对象销毁时就会指定该方法 public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContext销毁了"); } } |
2.HttpSessionListener:主要可以用来检测网站的访问次数
|
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(se.getSession()+"销毁了"); } } |
3.ServletRequestListener:两个方法
|
import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; public class MyServletRequestListener implements ServletRequestListener { //统计某个资源的访问次数 public void requestDestroyed(ServletRequestEvent sre) { } public void requestInitialized(ServletRequestEvent sre) { } } |
b、三个类型对象域属性中增、删、改的监听器(3个)
ServletContextAttributeListener,
HttpSessionAttributeListener,
ServletRequestAttributeListener
c、感知型监听器(2个):监听自己何时被帮到session上,何时解绑了;何时被钝化了,何时被活化了(序列化到某个存储设置中)。
注意:这种监听器不需要注册。某个javabean实现这些接口后就可以监听何时被绑定、解绑或被激活或钝化。
HttpSessionBindingListener:实现该接口的类,能检测自己何时被Httpsession绑定,和解绑
HttpSessionActivationListener:实现该接口的类(要求些javabean必须是实现了Serializable接口的),能监测自己何时随着HttpSession一起激活和钝化。
ServletListener对象学习笔记的更多相关文章
- javaScript 对象学习笔记
javaScript 对象学习笔记 关于对象,这对我们软件工程到学生来说是不陌生的. 因为这个内容是在过年学到,事儿多,断断续续,总感觉有一丝不顺畅,但总结还是要写一下的 JavaScript 对象 ...
- ASP.NET本质论第二章应用程序对象学习笔记1
1.请求的处理参数—上下文对象HttpContext 1) 针对每一次请求,ASP.NET将创建一个处理这次请求所使用的HttpContext对象实例,这个对象实例将用来在ASP.NET服务器的处理过 ...
- JavaScript 对象 之继承对象 学习笔记
假设,我们有个这样的需求: 两个种族,每个种族都有 名字.血量(默认200).行为(行为有 跳跃.移动速度 这些属性)等共有属性. 人族能量值比兽人多10%,兽人血量比人族多10%. 职业有战士和法师 ...
- JavaScript中Date(日期对象),Math对象--学习笔记
Date对象 1.什么是Date对象? 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 语法:var Udate=new Date(); 注:初始值为当前时间(当前电脑系统 ...
- javaScript对象学习笔记(一)
一.什么是对象 对象: JavaScript的一种基本数据类型 对象是属性的无序集合,每个属性都是一个名/值对 JavaScript中的事物都是对象:字符串.数值.数组.函数... JavaScrip ...
- java——对象学习笔记
1.面向对象(OOP)的三大特性 对象的行为(behavior):可以对对象施加哪些操作,或者可以对对象施加哪些方法. 对象的状态(state):当施加那些方法后,对象如何响应. 对象标识(ident ...
- python_类与对象学习笔记
class Phone: #手机属性===>类属性 # color='black' # price=4500 # brand='oppo' # size='5.5' #参数化-魔法方法--初始化 ...
- OpenGL学习笔记3——缓冲区对象
在GL中特别提出了缓冲区对象这一概念,是针对提高绘图效率的一个手段.由于GL的架构是基于客户——服务器模型建立的,因此默认所有的绘图数据均是存储在本地客户端,通过GL内核渲染处理以后再将数据发往GPU ...
- Redis学习笔记一:数据结构与对象
1. String(SDS) Redis使用自定义的一种字符串结构SDS来作为字符串的表示. 127.0.0.1:6379> set name liushijie OK 在如上操作中,name( ...
随机推荐
- Windows 2012建立域控(AD DS)详解
Active Directory概述: 使用 Active Directory(R) 域服务 (AD DS) 服务器角色,可以创建用于用户和资源管理的可伸缩.安全及可管理的基础机构, ...
- JavaWeb程序连接SQLserver数据库
声明:一直以来都以为javaweb程序连接数据库是一个很高大上很难的问题,结果今天学习了一下,不到两个小时就解决了,所以总结一篇博客. JavaWeb程序连接SQLserver数据库分为一下步骤: 1 ...
- 7_SQL Server通过代码删除数据
--通过代码方式删除数据select *from Employee --第一种删除方式,数据没了,表还在,id接着删除前的id继续加1delete from Employee where EmpId ...
- 浏览器控制台console的使用
前天在团队项目中因为产品需求在提交订单的时候需要多个页面的数据作为提交接口的参数,这种需求让人醉醉的,项目用angular来做的,没办法只能用全局变量来定义要交互的值和localStorage来临时的 ...
- 【ACM小白成长撸】--贪婪法解硬币找零问题
question:假设有一种货币,它有面值为1分.2分.5分和1角的硬币,最少需要多少个硬币来找出K分钱的零钱.按照贪婪法的思想,需要不断地使用面值最大的硬币.如果找零的值小于最大的硬币值,则尝试第二 ...
- [Unity 设计模式]桥接模式(BridgePattern)
1.前言 继上一讲IOC模式的基础上继续本讲桥接模式,笔者感觉桥接模式是23种设计模式中桥接模式是最好用但也是最难理解的设计模式之一,23中设计模式就好武侠剧中一本武功秘籍,我们在工作过程中想要熟练运 ...
- VMware bridge 桥接方式连接internet
经过反复测试,关于VMware内虚拟机(包括ubuntu linux和windows)连接internet 目前的结论是 使用bridge方式时,VMware相当于一个交换机(switch),虚拟机和 ...
- [置顶]
基于FPGA的VGA简易显存设计&NIOS ii软核接入
项目简介 本项目基于Altera公司的Cyclone IV型芯片,利用NIOS II软核,2-port RAM与时序控制模块,实现64*48分辨率的显存(再大的显存板载资源m9k不够用) 实现效果如下 ...
- 数据结构学习:KMP模式匹配算法
有关KMP的算法具体的实现网上有很多,不具体阐述.这里附上c的实现. 谈谈我自己的理解.KMP相较于朴素算法,其主要目的是为了使主串中的遍历参数i不回溯,而直接改变目标串中的遍历参数j. 比如说要是目 ...
- 微信小程序wx.navigateTo层叠5次限制,特殊情况的建议
小程序页面的实例使用栈的数据结构存储,栈内元素最多5个(换一种方式说,就是用户最多能点击5次返回),微信小程序能在栈中相对高层某个页面调用其他相对低层的页面实例的方法. 小程序三种页面跳转API 的区 ...