监听器的使用场景:

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

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

控制台打印:

最新文章

  1. 深度学习框架 Torch 7 问题笔记
  2. JavaScript基础系列(变量与类型)
  3. Protocol and Delegate协议和代理
  4. attilax.java 注解的本质and 使用最佳实践(3)O7
  5. poj 3061 Subsequence
  6. logback日志文件需要注意点
  7. Android ScaleAnimation类:尺寸变化动画类
  8. JavaScript Client-Side
  9. 6--OC--封装 继承 多态
  10. java获取泛型信息
  11. 通过this()调用有参构造方法
  12. eclipse中将一个项目作为library导入另一个项目中
  13. C++基础算法学习——猜假币
  14. Shell基础知识(二)
  15. php file_get_contents fopen 连接远程文件
  16. 设计模式(23)--Visitor--访问者模式--行为型
  17. java BeanUtils.copyProperties
  18. 四人小组:vip会员管理系统
  19. 数据库-mysql数据类型
  20. signal模块简介

热门文章

  1. JAVA基础知识总结4(面向对象特征之一:封装)
  2. HTML5+JavaScript动画基础 完整版 中文pdf扫描版
  3. ProtoBuf练习(二)
  4. Linux命令使用
  5. Permutations and Permutations II
  6. go语言web开发框架_Iris框架讲解(六):Session的使用和控制
  7. hdu3887 Counting Offspring
  8. 我的省选 Day -5
  9. for in在对象和数组中的应用
  10. Unity 行为树-管理