Dev_Nick

导航

监听器

概念

监听器:主要是用来监听特定对象的创建或销毁、属性的变化的!是一个实现特定接口的普通java类!

监听器的使用场景

自己创建自己用 (不用监听), 别人创建自己用 (需要监听)。

servlet需要监听的对象及监听内容

1、监听对象

  request / session / servletContext

2、监听内容

  对象的创建和销毁、属性的变化(增删改)。

常用监听器接口

一、监听对象创建/销毁的监听器接口

         Interface ServletRequestListener     监听request对象的创建或销毁

         Interface HttpSessionListener        监听session对象的创建或销毁

         Interface ServletContextListener     监听servletContext对象的创建或销毁

二、监听对象属性的变化

         Interface ServletRequestAttributeListener 监听request对象属性变化: 添加、移除、修改

         Interface HttpSessionAttributeListener    监听session对象属性变化: 添加、移除、修改

    Interface ServletContextAttributeListener  监听servletContext对象属性变化

三、session相关监听器

         Interface HttpSessionBindingListener   监听对象绑定到session上的事件     

     Interface HttpSessionActivationListener(了解) 监听session序列化及反序列化的事件

代码示例

1、ServletRequestListener    

 1 import javax.servlet.ServletRequestEvent;
 2 import javax.servlet.ServletRequestListener;
 3 
 4 /**
 5  *  监听request对象的创建或销毁
 6  * @author Jie.Yuan
 7  *
 8  */
 9 public class MyRequestListener implements ServletRequestListener{
10 
11     // 对象销毁
12     @Override
13     public void requestDestroyed(ServletRequestEvent sre) {
14         // 获取request中存放的数据
15         Object obj = sre.getServletRequest().getAttribute("cn");
16         System.out.println(obj);
17         System.out.println("MyRequestListener.requestDestroyed()");
18     }
19 
20     // 对象创建
21     @Override
22     public void requestInitialized(ServletRequestEvent sre) {
23         System.out.println("MyRequestListener.requestInitialized()");
24     }
25 }
View Code

2、HttpSessionListener

 1 import javax.servlet.http.HttpSessionEvent;
 2 import javax.servlet.http.HttpSessionListener;
 3 
 4 /**
 5  * 监听Session对象创建、销毁
 6  * @author Jie.Yuan
 7  *
 8  */
 9 public class MySessionListener implements HttpSessionListener{
10 
11     // 创建
12     @Override
13     public void sessionCreated(HttpSessionEvent se) {
14         System.out.println("MySessionListener.sessionCreated()");
15     }
16 
17     // 销毁
18     @Override
19     public void sessionDestroyed(HttpSessionEvent se) {
20         System.out.println("MySessionListener.sessionDestroyed()");
21     }
22 
23 }
View Code

3、ServletContextListener    

 1 import javax.servlet.ServletContextEvent;
 2 import javax.servlet.ServletContextListener;
 3 
 4 /**
 5  * 监听ServletContext对象创建或销毁
 6  * @author Jie.Yuan
 7  *
 8  */
 9 public class MyServletContextListener implements ServletContextListener{
10 
11     @Override
12     public void contextDestroyed(ServletContextEvent sce) {
13         System.out.println("MyServletContextListener.contextDestroyed()");
14     }
15 
16     @Override
17     public void contextInitialized(ServletContextEvent sce) {
18         System.out.println("1..........MyServletContextListener.contextInitialized()");
19     }
20 
21 }
View Code

4、HttpSessionAttributeListener   

 1 import javax.servlet.http.HttpSession;
 2 import javax.servlet.http.HttpSessionAttributeListener;
 3 import javax.servlet.http.HttpSessionBindingEvent;
 4 
 5 /**
 6  * 监听session对象属性的变化
 7  * @author Jie.Yuan
 8  *
 9  */
10 public class MySessionAttrListener implements HttpSessionAttributeListener {
11 
12     // 属性添加
13     @Override
14     public void attributeAdded(HttpSessionBindingEvent se) {
15         // 先获取session对象
16         HttpSession session = se.getSession();
17         // 获取添加的属性
18         Object obj = session.getAttribute("userName");
19         // 测试
20         System.out.println("添加的属性:" + obj);
21     }
22 
23     // 属性移除
24     @Override
25     public void attributeRemoved(HttpSessionBindingEvent se) {
26         System.out.println("属性移除");
27     }
28 
29     // 属性被替换
30     @Override
31     public void attributeReplaced(HttpSessionBindingEvent se) {
32         // 获取sesison对象
33         HttpSession session = se.getSession();
34         
35         // 获取替换前的值
36         Object old = se.getValue();
37         System.out.println("原来的值:" + old);
38         
39         // 获取新值
40         Object obj_new = session.getAttribute("userName");
41         System.out.println("新值:" + obj_new);
42         
43     }
44 
45 }
View Code

5、HttpSessionBindingListener  

 1 import javax.servlet.http.HttpSessionBindingEvent;
 2 import javax.servlet.http.HttpSessionBindingListener;
 3 
 4 /**
 5  * 监听此对象绑定到session上的过程,需要实现session特定接口
 6  * @author Jie.Yuan
 7  *
 8  */
 9 public class Admin implements HttpSessionBindingListener {
10 
11     private int id;
12     private String name;
13     
14     public Admin() {
15         super();
16     }
17     public Admin(int id, String name) {
18         super();
19         this.id = id;
20         this.name = name;
21     }
22     
23     
24     // 构造函数
25     public int getId() {
26         return id;
27     }
28     public void setId(int id) {
29         this.id = id;
30     }
31     public String getName() {
32         return name;
33     }
34     public void setName(String name) {
35         this.name = name;
36     }
37     
38     // 对象放入session
39     @Override
40     public void valueBound(HttpSessionBindingEvent event) {
41         System.out.println("Admin对象已经放入session");
42     }
43     // 对象从session中移除
44     @Override
45     public void valueUnbound(HttpSessionBindingEvent event) {
46         System.out.println("Admin对象从session中移除!");
47     }
48     
49 }
View Code

6、测试代码

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3   <head>
 4     
 5     <title>My JSP 'index.jsp' starting page</title>
 6     <meta http-equiv="pragma" content="no-cache">
 7     <meta http-equiv="cache-control" content="no-cache">
 8     <meta http-equiv="expires" content="0">    
 9   </head>
10   
11   <body>
12       test!
13       <%
14       
15           //request.setAttribute("cn","China");
16           //session.invalidate();  //销毁session
17           
18           //session.setAttribute("userName","Jack");
19           //session.removeAttribute("userName");
20           //session.setAttribute("userName","Jack_new");
21           
22           session.setAttribute("userInfo",new Admin());
23           session.removeAttribute("userInfo");
24       %>
25   </body>
26 </html>
View Code

posted on 2017-06-01 19:40  Dev_Nick  阅读(235)  评论(0编辑  收藏  举报