servlet 几种 listener

ServletContextListener

Implementations of this interface recieve notifications about changes to the servlet context of the web application they are part of. To recieve notification events, the implementation class must be configured in the deployment descriptor for the web application.

——这个事件类作为Web应用服务的一部分,处理Web应用的 servlet上下文(context)的变化的通知。这可以解释为,好像有个人在服务器旁不断地通知我们服务器在发生什么事件。那当然需要监听者了。因 此,在通知上下文(context)初始化和销毁的时候,ServletContextListner非常有用。

ServletRequestListener

A ServletRequestListener can be implemented by the developer interested in being notified of requests coming in and out of scope in a web component. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.

——

注意:

<link href="...">
<script src="...">
<jsp:include ...>

这种代码均会出发ServletRequestListener。

HttpSessionListener

Implementations of this interface may are notified of changes to the list of active sessions in a web application. To recieve notification events, the implementation class must be configured in the deployment descriptor for the web application.

——当一个浏览器第一次访问网站的时候,J2EE应用服务器会新建一个 HttpSession对象,并触发HttpSession创建事件,如果注册了HttpSessionListener事件监听器,则会调用 HttpSessionListener事件监听器的sessionCreated方法。相反,当这个浏览器访问结束超时的时候,J2EE应用服务器会销 毁相应的HttpSession对象,触发HttpSession销毁事件,同时调用所注册HttpSessionListener事件监听器的 sessionDestroyed方法。

 

 

如果同时配置了ServletContextListener,HttpSessionListener,ServletRequestListener,容器启动时
会 先调用ServletContextListener的contextInitialized方法。然后当客户端有请求到来,会先调用 ServletRequestListener的requestInitialized方法,然后再调用HttpSessionListener的 sessionCreated方法,如果发生上面所说的页面链接了其它文件的情况,则会再次触发ServletRequestListener的 requestInitialized方法。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值