监听器的使用场景:

  ①:统计在线人数   ②:实现单一登录【一个账号只能在一台机器上登录】

Servlet中的8大监听器:

1.         ServletContextListener

[接口方法] contextInitialized()与contextDestroyed()

[接收事件] ServletContextEvent

[触发场景] 在Container加载Web应用程序时(例如启动Container之后),会调用contextInitialized(),而当容器移除Web应用程序时,会调用contextDestroy()方法。

2.         ServletContextAttributeListener

[接口方法] attributeAdded()、attributeReplaced()、attributeRemoved()

[接收事件] ServletContextAttributeEvent

[触发场景] 若有对象加入为application(ServletContext)对象的属性,则会调用attributeAdded(),同理在置换和移除属性时,会分别调用attributeReplaced()、attributeRemoved()。

3.         HttpSessionListener

[接口方法] sessionCreated()与sessionDestroyed()

[接收事件] HttpSessionEvent

[触发场景] 在session(HttpSession)对象建立或者消亡时,会分别调用这两个方法。

4.         HttpSessionAttributeListener

[接口方法] attributeAdded()、attributeReplaced()、attributeRemoved()

[接收事件] HttpSessionBindingEvent

[触发场景] 若有对象加入为session(HttpSession)对象的属性,则会调用attributeAdded(),同理在置换和移除属性时,会分别调用attributeReplaced()、attributeRemoved()。

5.         ServletRequestListener

[接口方法] requestInitialized()与requestDestroyed()

[接收事件] RequestEvent

[触发场景] 在request(HttpServletRequest)对象建立或者消亡时,会分别调用这两个方法。

6.         ServletRequestAttributeListener

[接口方法] attributeAdded()、attributeReplaced()、attributeRemoved()

[接收事件] HttpSessionBindingEvent

[触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会调用attributeAdded(),同理在置换和移除属性时,会分别调用attributeReplaced()、attributeRemoved()。

7.         HttpSessionBindingListener

[接口方法] valueBound()与valueUnbound()

[接收事件] HttpSessionBindingEvent

[触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入到session(HttpSession)对象的属性中,则会调用valueBound(),如果从session(HttpSession)对象的属性中移除,则会调用valueUnbound(),实现HttpSessionBindingListener接口的类别不需要在web.xml中设定。

8.         HttpSessionActivationListener

[接口方法] sessionDidActivate()与sessionWillPassivate()

[接收事件] HttpSessionEvent

[触发场景] Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或者负载均衡等原因而必须暂时储存至硬盘或其他存储器时(通过对象序列化),所作的动作称为Passivate,而硬盘或者储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以sessionDidActivate()与sessionWillPassivate()分别于Activate后和Passivate前调用。

除了HttpSessionBindingListener和HttpSessionActivationListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时调用对应的类别。

代码演示:

TestServlet.java

 public class TestServlet extends HttpServlet {

     /**
*
*/
private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行get请求");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行post请求");
}
}

TestListener.java

 public class TestListener implements ServletRequestListener{

     @Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
System.out.println("request销毁");
} @Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
System.out.println("request初始化");
}
}

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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ListenerDemo</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> <servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>cn.woo.servlet.TestServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/servlet/testServlet</url-pattern>
</servlet-mapping> <!-- 配置Listener:监听request的初始化和销毁 -->
<listener>
<listener-class>cn.woo.listener.TestListener</listener-class>
</listener> </web-app>

通过:http://localhost:8080/ListenerDemo/servlet/testServlet 访问Servlet

控制台打印:

Servlet高级部分Listener的更多相关文章

  1. Servlet的监听器Listener

    Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是 随web应用的启动而启动,只初始化一次,随web ...

  2. servlet 高级知识之Listener

    Listener,顾名思义,监听器.它可以监听客户端的请求.服务端的操作等. 通过监听器,可以自动激发一些操作,比如监听在线的用户的数量.当增加一个HttpSession时,就激发sessionCre ...

  3. [web]Servlet中的Listener和Filter

    建议先看看 ——> Servlet工作原理 一.Listener 在Tomcat服务中,Listener的设计是基于观察者模式的,目前在Servlet中提供6中两类事件的观察者接口,它们分别是: ...

  4. Servlet监听器(Listener)实例

    以下内容是翻译自http://www.journaldev.com/1945/servletcontextlistener-servlet-listener-example: 说明:web.xml的加 ...

  5. <<< web里面Servlet高级应用的基础介绍

    Servlet中的页面跳转?两种方式,实现跳转:内部跳转(请求转发).外部跳转(重定向)内部跳转(请求转发)特点:在服务器内部完成页面之间的跳转:请求只有一次:浏览器地址不会改变.request.ge ...

  6. Servlet高级

    1. 获取初始化参数 在web.xml中配置Servlet时,可以配置一些初始化参数.而在Servlet中可以通过ServletConfig接口提供的方法来取得这些参数. index.jsp < ...

  7. Servlet高级应用---Servlet与缓存

    一]设置缓存文件的有效日期        重点方法:            HttpServletRequest类:                    1>String getRequest ...

  8. servlet 高级知识之Filter

    Filter叫做拦截器, 对目标资源拦截,拦截HTTP请求和HTTP响应,本质是对url进行拦截. 与serlvet不同的是, Filter的初始化是随着服务器启动而启动. 在Filter接口中定义了 ...

  9. Servlet高级部分Filter(过滤器)

    一:Filter称之为"过滤器",用在Servlet外,对request和response进行修改.它是AOP(面向切面编程思想的一种体现),Filter中有一个FilterCha ...

随机推荐

  1. 树莓派 Learning 002 装机后必要的操作 --- 10 实现本地电脑与远程桌面之间复制粘贴(传输)文件

    树莓派 装机后必要的操作 - 实现本地电脑与远程桌面之间复制粘贴(传输)文件 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 PC端系统:w ...

  2. android studio在windows上设置git/ssh

    windows果然是与众不同的,凡事都要那么麻烦一点点(当然..是对程序员来说..) 一开始,我想用cygwin里的git,就省得我再多装一套软件,配置也可以统一,但事实证明不行 在android s ...

  3. 《精通Spring4.X企业应用开发实战》读后感第五章(基于Java类的配置)

  4. 什么是消息循环,一个简单的win32程序如何运行?

    预备知识 1.什么是句柄? (HANDLE) 在win32编程中有各种句柄,那么什么是句柄呢? #define DECLARE_HANDLE(name) struct name##_ { int un ...

  5. codeforces 367B

    题目代码到是不难但是很难想通题目的解决方法. #include<iostream> using namespace std; ]; int main() { int n,m; while( ...

  6. ProtoBuf练习

    环境设置 项目地址 https://github.com/silvermagic/ProtoBufDev.git 操作系统 64位 Fedora 24 安装protobuf $ git clone h ...

  7. [CentOS7] SELinux

    声明:本文主要总结自:鸟哥的Linux私房菜-第十六章.程序管理與 SELinux 初探,如有侵权,请通知博主 SELinux = Security Enhanced Linux 传统的文件权限与账号 ...

  8. JSP-用网页输出乘法表 三角形及菱形

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. Polycarp's Pockets(思维)

    Polycarp has nn coins, the value of the ii-th coin is aiai. Polycarp wants to distribute all the coi ...

  10. 1088 Rational Arithmetic(20 分)

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...