web Listener介绍

2024-05-13 13:48
文章标签 介绍 web listener

本文主要是介绍web Listener介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

当web运用在web容器中运行时,会产生各种事件(如web启动,停止,用户的session创建,销毁,请求到达等)这些对于web事件开发者都可以监听到。

使用Listener只需要2个步骤

 1)定义Lisenter实现类

[java]  view plain copy
  1. package com.listener;  
  2.   
  3. import javax.servlet.ServletContextEvent;  
  4. import javax.servlet.ServletContextListener;  
  5. import org.apache.log4j.*;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  9.   
  10. import com.quartz.QuartzServiceImpl;  
  11. import com.util.DateUtil;  
  12.   
  13. public class WebStartListener implements ServletContextListener{  
  14.     Logger logger = Logger.getLogger(WebStartListener.class);  
  15.   
  16.     @Override  
  17.     public void contextInitialized(ServletContextEvent sce) {  
  18.         // TODO Auto-generated method stub  
  19.         logger.warn("启动");  
  20.           
  21. //      ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:com/springResource/*.xml"});  
  22. //      QuartzServiceImpl quartzService = (QuartzServiceImpl)springContext.getBean("quartzService");    
  23. //      quartzService.schedule("name=testQuartz2",DateUtil.parse("2012-12-12 23:42:00"));   
  24.     }  
  25.       
  26.     @Override  
  27.     public void contextDestroyed(ServletContextEvent sce) {  
  28.         // TODO Auto-generated method stub  
  29.         logger.warn("关闭");  
  30.     }  
  31.   
  32.   
  33. }  


2)配置web.xml

[html]  view plain copy
  1.  <!-- web服务器启动监听 -->  
  2. lt;listener>  
  3. <listener-class>com.listener.WebStartListener</listener-class>  
  4. lt;/listener>  

 

 

 

servlet内置listener

l ServletContextListener
    [接口方法] contextInitialized()与 contextDestroyed()
    [接收事件] ServletContextEvent
    [触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。
    l ServletContextAttributeListener
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
    [接收事件] ServletContextAttributeEvent
    [触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。
    l HttpSessionListener
    [接口方法] sessionCreated()与sessionDestroyed ()
    [接收事件] HttpSessionEvent
    [触发场景] 在session(HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。
    l HttpSessionAttributeListener
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
    [接收事件] HttpSessionBindingEvent
    [触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
    l ServletRequestListener
    [接口方法] requestInitialized()与 requestDestroyed()
    [接收事件] RequestEvent
    [触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。
    l ServletRequestAttributeListener
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
    [接收事件] HttpSessionBindingEvent
    [触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
    l HttpSessionBindingListener
    [接口方法] valueBound()与valueUnbound()
    [接收事件] HttpSessionBindingEvent
    [触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入至session(HttpSession)对象的属性中,则会呼叫 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会呼叫valueUnbound(),实现HttpSessionBindingListener接口的类别不需在web.xml中设定。
    l HttpSessionActivationListener
    [接口方法] sessionDidActivate()与 sessionWillPassivate()
    [接收事件] HttpSessionEvent
    [触发场景] Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。

这篇关于web Listener介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/985879

相关文章

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

setsid 命令工作原理和使用案例介绍

《setsid命令工作原理和使用案例介绍》setsid命令在Linux中创建独立会话,使进程脱离终端运行,适用于守护进程和后台任务,通过重定向输出和确保权限,可有效管理长时间运行的进程,本文给大家介... 目录setsid 命令介绍和使用案例基本介绍基本语法主要特点命令参数使用案例1. 在后台运行命令2.

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

Python Web框架Flask、Streamlit、FastAPI示例详解

《PythonWeb框架Flask、Streamlit、FastAPI示例详解》本文对比分析了Flask、Streamlit和FastAPI三大PythonWeb框架:Flask轻量灵活适合传统应用... 目录概述Flask详解Flask简介安装和基础配置核心概念路由和视图模板系统数据库集成实际示例Stre

zookeeper端口说明及介绍

《zookeeper端口说明及介绍》:本文主要介绍zookeeper端口说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、zookeeper有三个端口(可以修改)aVNMqvZ二、3个端口的作用三、部署时注意总China编程结一、zookeeper有三个端口(可以

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方