web.xml里的顺序为:context-param->listener->filter->servlet

监听器是需要新建一个类,然后按监听的对象继承:ServletContext、HttpSession、ServletRequest中的一个


监听ServletContext生命周期的Listener

 //ServletContext  lifecycle changes
public interface ServletContextListener extends EventListener { //initialized
public void contextInitialized(ServletContextEvent sce);  //destroyed
public void contextDestroyed(ServletContextEvent sce);
}

监听ServletContext属性的Listener

public interface ServletContextAttributeListener extends EventListener {

    /**
* Receives notification that an attribute has been added to the
* ServletContext.
*
* @param event the ServletContextAttributeEvent containing the
* ServletContext to which the attribute was added, along with the
* attribute name and value
*/
public void attributeAdded(ServletContextAttributeEvent event); /**
* Receives notification that an attribute has been removed
* from the ServletContext.
*
* @param event the ServletContextAttributeEvent containing the
* ServletContext from which the attribute was removed, along with
* the attribute name and value
*/
public void attributeRemoved(ServletContextAttributeEvent event); /*
* Receives notification that an attribute has been replaced
* in the ServletContext.
*
* @param event the ServletContextAttributeEvent containing the
* ServletContext in which the attribute was replaced, along with
* the attribute name and its old value
*/
public void attributeReplaced(ServletContextAttributeEvent event);
}

监听HttpSession生命周期的Listener

//about HttpSession lifecycle changes.
public interface HttpSessionListener extends EventListener { /**
* Receives notification that a session has been created.
*
* @param se the HttpSessionEvent containing the session
*/
public void sessionCreated(HttpSessionEvent se); /**
* Receives notification that a session is about to be invalidated.
*
* @param se the HttpSessionEvent containing the session
*/
public void sessionDestroyed(HttpSessionEvent se); }

监听HttpSession属性的Listener

public interface HttpSessionAttributeListener extends EventListener {

    /**
* Receives notification that an attribute has been added to a
* session.
*
* @param event the HttpSessionBindingEvent containing the session
* and the name and value of the attribute that was added
*/
public void attributeAdded(HttpSessionBindingEvent event); /**
* Receives notification that an attribute has been removed from a
* session.
*
* @param event the HttpSessionBindingEvent containing the session
* and the name and value of the attribute that was removed
*/
public void attributeRemoved(HttpSessionBindingEvent event); /**
* Receives notification that an attribute has been replaced in a
* session.
*
* @param event the HttpSessionBindingEvent containing the session
* and the name and (old) value of the attribute that was replaced
*/
public void attributeReplaced(HttpSessionBindingEvent event); }

监听ServletReques生命周期的tListener

public interface ServletRequestListener extends EventListener {

    /**
* Receives notification that a ServletRequest is about to go out
* of scope of the web application.
*
* @param sre the ServletRequestEvent containing the ServletRequest
* and the ServletContext representing the web application
*/
public void requestDestroyed(ServletRequestEvent sre); /**
* Receives notification that a ServletRequest is about to come
* into scope of the web application.
*
* @param sre the ServletRequestEvent containing the ServletRequest
* and the ServletContext representing the web application
*/
public void requestInitialized(ServletRequestEvent sre);
}

监听ServletReques属性的Listener

public interface ServletRequestAttributeListener extends EventListener {

    /**
* Receives notification that an attribute has been added to the
* ServletRequest.
*
* @param srae the ServletRequestAttributeEvent containing the
* ServletRequest and the name and value of the attribute that was
* added
*/
public void attributeAdded(ServletRequestAttributeEvent srae); /**
* Receives notification that an attribute has been removed from the
* ServletRequest.
*
* @param srae the ServletRequestAttributeEvent containing the
* ServletRequest and the name and value of the attribute that was
* removed
*/
public void attributeRemoved(ServletRequestAttributeEvent srae); /**
* Receives notification that an attribute has been replaced on the
* ServletRequest.
*
* @param srae the ServletRequestAttributeEvent containing the
* ServletRequest and the name and (old) value of the attribute
* that was replaced
*/
public void attributeReplaced(ServletRequestAttributeEvent srae);
}

HttpSessionBindingListener:该接口有两个方法,valueBound和valueUnbound,实现该接口的对象要new以后放在session里。

HttpSessionActivationListener :实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化(反序列化)sessionDidActivate和钝化(序列化)sessionWillPassivate的事件

HttpSessionBindingListener和HttpSessionListener区别是前者要创建对象后放入session中,后者创建一次

学习java web中的listener的更多相关文章

  1. 疯狂学习java web

    因工作需要,疯狂学习java web,只是这么多年一直从事C++开发,突然之间要接手同事的那么一大堆代码,真有无从下手的感觉,首先是要学习html,然后是js, 然后是jsp,当然还有各种框架,想想就 ...

  2. 零基础如何系统学习Java Web

    零基础如何系统学习Java Web?   我来给你说一说 你要下决心,我要转行做开发,这样你才能学成. 你要会打字,我公司原来有一个程序员,打字都是两个手一指禅,身为程序员你一指禅怎么写出的代码,半个 ...

  3. Java Web 中 过滤器与拦截器的区别

    过滤器,是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法u ...

  4. JAVA WEB 中的编码分析

    JAVA WEB 中的编码分析 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src {background-co ...

  5. Java web中常见编码乱码问题(一)

    最近在看Java web中中文编码问题,特此记录下. 本文将会介绍常见编码方式和Java web中遇到中文乱码问题的常见解决方法: 一.常见编码方式: 1.ASCII 码 众所周知,这是最简单的编码. ...

  6. Java web中常见编码乱码问题(二)

    根据上篇记录Java web中常见编码乱码问题(一), 接着记录乱码案例: 案例分析:   2.输出流写入内容或者输入流读取内容时乱码(内容中有中文) 原因分析: a. 如果是按字节写入或读取时乱码, ...

  7. 深入分析Java Web中的编码问题

    编码问题一直困扰着我,每次遇到乱码或者编码问题,网上一查,问题解决了,但是实际的原理并没有搞懂,每次遇到,都是什么头疼. 决定彻彻底底的一次性解决编码问题. 1.为什么要编码 计算机的基本单元是字节, ...

  8. 解决java web中safari浏览器下载后文件中文乱码问题

    解决java web中safari浏览器下载后文件中文乱码问题 String fileName = "测试文件.doc"; String userAgent = request.g ...

  9. Java Web 中使用ffmpeg实现视频转码、视频截图

    Java Web 中使用ffmpeg实现视频转码.视频截图 转载自:[ http://www.cnblogs.com/dennisit/archive/2013/02/16/2913287.html  ...

随机推荐

  1. delphi String 与 Stream的互转

    stream1   :=   TStringStream.create(str); str   :=   TStringStream(stream1).DataString; Stream 是抽像类, ...

  2. LOJ 2980 「THUSCH 2017」大魔法师——线段树

    题目:https://loj.ac/problem/2980 线段树维护矩阵. 然后是 30 分.似乎是被卡常了?…… #include<cstdio> #include<cstri ...

  3. CTF | bugku | 字符?正则?

    做题链接 一个详细讲正则的网址1 一个详细讲正则的网址2 代码如下 <?php highlight_file('2.php'); $key='KEY{********************** ...

  4. [CSP-S模拟测试]:Miner(欧拉路)

    题目背景 $And\ the\ universe\ said\ you\ are\ the\ daylight \\ And\ the\ universe\ said\ you\ are\ the\ ...

  5. (65)C# 任务

    1.启动任务 //Framework4.5新增的Task.Run开启一个任务,Run方法中传入一个Action委托 Task.Run(()=> { Thread.Sleep(); Console ...

  6. Windows 08R2 IIS网站架设

    目录 目录 配置和安装IIS 环境设置 安装IIS服务器 网站的站点目录和欢迎页面 配置和安装IIS IIS是Windows的网站服务器,所以配置IIS服务的前提是需要一个网址.和DNS域名并添加主机 ...

  7. USACO 6.3 章节 你对搜索和剪枝一无所知QAQ

    emmm........很久很久以前 把6.2过了 所以emmmmmm 直接跳过 ,从6.1到6.3吧 Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值 ...

  8. python类与对象

    1.  class命名规范:首字母大写,驼峰命名法 2.  class 里面函数括号里面带(self)说明这是一个实例,调用实例方法需要用实例调用 class Teacher: def  test_1 ...

  9. 微信小程序发送红包功能。填坑记录

    微信官方文档 1.开通条件 (1)商户号已入驻90日 (2)商户号有连续30天正常交易 (3)只有企业资质的商户才有资格申请 2.注意事项 (1)目前小程序红包仅支持用户微信扫码打开小程序 (2)小程 ...

  10. JPA、Hibernate、Spring Data JPA 的关系,你懂吗?

    来源:https://my.oschina.net/u/3080373/blog/1828589 什么是JPA? 全称Java Persistence API,可以通过注解或者XML描述[对象-关系表 ...