服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 基于Listener监听器生命周期(详解)

基于Listener监听器生命周期(详解)

2021-01-21 13:11小Cai先森 Java教程

下面小编就为大家带来一篇基于Listener监听器生命周期(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、listener生命周期

listener是web三大组件之一,是servlet监听器,用来监听请求,监听服务端的操作。

listener分为:(都是接口类,必须实现相应方法)

1.生命周期监听器(3个)

?
1
2
3
4
5
6
7
8
9
servletcontextlistener
requestdestroyed 在容器启动时被调用(在servlet被实例化前执行)
requestinitialized 在容器销毁时调用(在servlet被销毁后执行)
httpsessionlistener
sessioncreated 在httpsession创建后调用
sessiondestroyed 在httpsession销毁前调用(执行session.invalidate();方法)
servletrequestlistener
requestdestroyed 在request对象创建后调用(发起请求)
requestinitialized 在request对象销毁前调用(请求结束)

2.属性变化监听器(3个)

?
1
2
3
4
5
6
7
8
9
10
11
attributeadded(servletcontextattributeevent event)向appliction中添加属性时调用
attributeremoved(servletcontextattributeevent event)从appliction中删除属性时调用
attributereplaced(servletcontextattributeevent event)替换application中的属性时调用
httpsessionattributelistener
attributeadded(httpsessionbindingevent event)
attributeremoved(httpsessionbindingevent event)
attributereplaced(httpsessionbindingevent event)
servletrequestattributelistener
attributeadded(servletrequestattributeevent event)
attributeremoved(servletrequestattributeevent event)
attributereplaced(servletrequestattributeevent event)

以上监听器接口除了传参不同,方法名都是一样的。分别监听application,session,request对象的属性变化。

3.session中指定类属性变化监听器(2)

?
1
2
3
4
5
6
httpsessionbindinglistener 
valuebound(httpsessionbindingevent event) 当该类实例设置进session域中时调用
valueunbound(httpsessionbindingevent event) 当该类的实例从session域中移除时调用
httpsessionactivationlistener 
sessionwillpassivate(httpsessionevent se)
sessiondidactivate(httpsessionevent se)

二、测试范例

1.生命周期监听:

servletcontentattribute_listener.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class servletcontentattribute_listener implements servletcontextlistener {
 /**
  * servletcontextlistener实现方法
  * @param sce
  */
 public void contextinitialized(servletcontextevent sce) {
  system.out.println("servletcontextlistener初始化");
 }
 
 public void contextdestroyed(servletcontextevent sce) {
  system.out.println("servletcontextlistener销毁");
 }
}

其他两个监听器类似,不在重复贴出。

在web.xml中配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 监听器 -->
 <!-- servlet监听器 -->
 <listener>
 <listener-class>study.mylistener.servletcontentattribute_listener</listener-class>
 </listener>
 
 <!-- session监听器 -->
 <listener>
 <listener-class>study.mylistener.httpsessionattribute_listener</listener-class>
 </listener>
 
 <!-- request监听器-->
 <listener>
 <listener-class>study.mylistener.servletrequestattribute_listener</listener-class>
 </listener>

运行结果:

 基于Listener监听器生命周期(详解)

  基于Listener监听器生命周期(详解)

2.属性监听:

servletcontentattribute_listener.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class servletcontentattribute_listener implements servletcontextattributelistener{
 
 /**
  * servletcontextattributelistener实现方法
  * @param event
  */
 public void attributeadded(servletcontextattributeevent event) {
  string meg = messageformat.format("servletcontent添加属性:{0},属性值:{1}",event.getname(),event.getvalue());
  system.out.println(meg);
 }
 
 public void attributeremoved(servletcontextattributeevent event) {
  string meg = messageformat.format("servletcontent删除属性:{0},属性值:{1}",event.getname(),event.getvalue());
  system.out.println(meg);
 }
 
 public void attributereplaced(servletcontextattributeevent event) {
  string meg = messageformat.format("servletcontent替换属性:{0},属性值:{1}",event.getname(),event.getvalue());
  system.out.println(meg);
 }
 
}

另外两个监听器类似,不在赘诉。接下来用jsp页面测试

listenerdemo.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%--
 created by intellij idea.
 user: administrator
 date: 2017/10/17
 time: 15:28
 to change this template use file | settings | file templates.
--%>
<%@ page contenttype="text/html;charset=utf-8" language="java" %>
<html>
<head>
 <title>监听器设置</title>
</head>
<body>
 <%
  /**
   * servlet监听
   */
  application.setattribute("name","changxiang");
  application.setattribute("name","小cai先森");
  application.removeattribute("name");
 
  /**
   * session监听
   */
  session.setattribute("sessionname","changxiang");
  session.setattribute("sessionname","小cai先森");
  session.removeattribute("sessionname");
  session.invalidate();
  /**
   * request监听
   */
  request.setattribute("requestname","changxiang");
  request.setattribute("requestname","小cai先森");
  request.removeattribute("requestname");
 %>
</body>
</html>

执行结果如下:

 基于Listener监听器生命周期(详解)

注意:其中遇到一个问题:就是在启动tomcat的时候servletcontextlistener监听执行了两次,最后删除掉server.xml中 context 的手动配置,这样就不会加载两次了。

以上这篇基于listener监听器生命周期(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/caijh/archive/2017/10/17/7683007.html

延伸 · 阅读

精彩推荐