zoukankan      html  css  js  c++  java
  • 监听器(Listener)学习(二)

    一、监听域对象中属性的变更的监听器

      域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。
      这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

    1.1、attributeAdded 方法

      当向被监听对象中增加一个属性时,web容器就调用事件监听器的attributeAdded方法进行响应,这个方法接收一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象
      各个域属性监听器中的完整语法定义为:

     public void attributeAdded(ServletContextAttributeEvent scae)
     public void attributeAdded(HttpSessionBindingEvent  hsbe)
     public void attributeAdded(ServletRequestAttributeEvent srae)

    1.2、attributeRemoved 方法

      当删除被监听对象中的一个属性时,web容器调用事件监听器的attributeRemoved方法进行响应
      各个域属性监听器中的完整语法定义为:

    1 public void attributeRemoved(ServletContextAttributeEvent scae)
    2 public void attributeRemoved (HttpSessionBindingEvent  hsbe)
    3 public void attributeRemoved (ServletRequestAttributeEvent srae)

    1.3、attributeReplaced 方法

      当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的attributeReplaced方法进行响应
      各个域属性监听器中的完整语法定义为:

    1 public void attributeReplaced(ServletContextAttributeEvent scae)
    2 public void attributeReplaced (HttpSessionBindingEvent  hsbe)
    3 public void attributeReplaced (ServletRequestAttributeEvent srae)

    1.4、ServletContextAttributeListener监听器范例

      编写ServletContextAttributeListener监听器监听ServletContext域对象的属性值变化情况,代码如下:

    package com.web.listener;
    
    import java.text.MessageFormat;
    
    import javax.servlet.ServletContextAttributeEvent;
    import javax.servlet.ServletContextAttributeListener;
    
    /**
    * @ClassName: MyServletContextAttributeListener
    * @Description: ServletContext域对象中属性的变更的事件监听器
    * @author: hdb
    * @date: 2014-9-11 下午10:53:04
    *
    */ 
    public class MyServletContextAttributeListener implements
            ServletContextAttributeListener {
    
        @Override
        public void attributeAdded(ServletContextAttributeEvent scab) {
            String str =MessageFormat.format(
                    "ServletContext域对象中添加了属性:{0},属性值是:{1}"
                    ,scab.getName()
                    ,scab.getValue());
            System.out.println(str);
        }
    
        @Override
        public void attributeRemoved(ServletContextAttributeEvent scab) {
            String str =MessageFormat.format(
                    "ServletContext域对象中删除属性:{0},属性值是:{1}"
                    ,scab.getName()
                    ,scab.getValue());
            System.out.println(str);
        }
    
        @Override
        public void attributeReplaced(ServletContextAttributeEvent scab) {
            String str =MessageFormat.format(
                    "ServletContext域对象中替换了属性:{0}的值"
                    ,scab.getName());
            System.out.println(str);
        }
    }

      在web.xml文件中注册监听器

      <listener>
           <description>MyServletContextAttributeListener监听器</description>
           <listener-class>com.web.listener.MyServletContextAttributeListener</listener-class>
       </listener>

      编写ServletContextAttributeListenerTest.jsp测试页面:

    <%@ page language="java" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML>
    <html>
      <head>
        <title>ServletContextAttributeListener监听器测试</title>
      </head>
      
      <body>
            <%
               //往application域对象中添加属性
               application.setAttribute("name", "hdb");
              //替换application域对象中name属性的值
               application.setAttribute("name", "coosee");
               //移除application域对象中name属性
               application.removeAttribute("name");
               %>
      </body>
    </html>

      从运行结果中可以看到,ServletContextListener监听器成功监听到了ServletContext域对象(application)中的属性值的变化情况。

    1.5、ServletRequestAttributeListener和HttpSessionAttributeListener监听器范例

      编写监听器监听HttpSession和HttpServletRequest域对象的属性值变化情况,代码如下:

    package com.web.listener;
    
    import java.text.MessageFormat;
    
    import javax.servlet.ServletRequestAttributeEvent;
    import javax.servlet.ServletRequestAttributeListener;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    
    public class MyRequestAndSessionAttributeListener implements
            HttpSessionAttributeListener, ServletRequestAttributeListener {
    
        @Override
        public void attributeAdded(ServletRequestAttributeEvent srae) {
            String str =MessageFormat.format(
                    "ServletRequest域对象中添加了属性:{0},属性值是:{1}"
                    ,srae.getName()
                    ,srae.getValue());
            System.out.println(str);
        }
    
        @Override
        public void attributeRemoved(ServletRequestAttributeEvent srae) {
            String str =MessageFormat.format(
                    "ServletRequest域对象中删除属性:{0},属性值是:{1}"
                    ,srae.getName()
                    ,srae.getValue());
            System.out.println(str);
        }
    
        @Override
        public void attributeReplaced(ServletRequestAttributeEvent srae) {
            String str =MessageFormat.format(
                    "ServletRequest域对象中替换了属性:{0}的值"
                    ,srae.getName());
            System.out.println(str);
        }
    
        @Override
        public void attributeAdded(HttpSessionBindingEvent se) {
            String str =MessageFormat.format(
                    "HttpSession域对象中添加了属性:{0},属性值是:{1}"
                    ,se.getName()
                    ,se.getValue());
            System.out.println(str);
        }
    
        @Override
        public void attributeRemoved(HttpSessionBindingEvent se) {
            String str =MessageFormat.format(
                    "HttpSession域对象中删除属性:{0},属性值是:{1}"
                    ,se.getName()
                    ,se.getValue());
            System.out.println(str);
        }
    
        @Override
        public void attributeReplaced(HttpSessionBindingEvent se) {
            String str =MessageFormat.format(
                    "HttpSession域对象中替换了属性:{0}的值"
                    ,se.getName());
            System.out.println(str);
        }
    }

      在web.xml文件中注册监听器

      <listener>
           <description>MyRequestAndSessionAttributeListener监听器</description>
           <listener-class>com.web.listener.MyRequestAndSessionAttributeListener</listener-class>
       </listener>

      编写RequestAndSessionAttributeListenerTest.jsp测试页面

    <%@ page language="java" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML>
    <html>
      <head>
        <title>RequestAndSessionAttributeListener监听器测试</title>
      </head>
      
      <body>
        <%
               //往session域对象中添加属性
               session.setAttribute("aa", "bb");
              //替换session域对象中aa属性的值
               session.setAttribute("aa", "xx");
               //移除session域对象中aa属性
               session.removeAttribute("aa");
               
               //往request域对象中添加属性
               request.setAttribute("aa", "bb");
              //替换request域对象中aa属性的值
               request.setAttribute("aa", "xx");
               //移除request域对象中aa属性
               request.removeAttribute("aa");
        %>
      </body>
    </html>

      运行结果如下:

      

      从运行结果中可以看到,HttpSessionAttributeListener监听器和ServletRequestAttributeListener成功监听到了HttpSession域对象和HttpServletRequest域对象的属性值变化情况。

    二、感知Session绑定的事件监听器

      保存在Session域中的对象可以有多种状态:绑定(session.setAttribute("bean",Object))到Session中;从 Session域中解除(session.removeAttribute("bean"))绑定;随Session对象持久化到一个存储设备中;随Session对象从一个存储设备中恢复
      Servlet 规范中定义了两个特殊的监听器接口"HttpSessionBindingListenerHttpSessionActivationListener"来帮助JavaBean 对象了解自己在Session域中的这些状态: ,实现这两个接口的类不需要 web.xml 文件中进行注册

      针对某些POJO类,可以通过实现HttpSessionBindingListener接口,监听POJO类对象的事件。例如:

    public class User implements HttpSessionBindingListener,Serializable{
    
        private String username;
        private String password;
        
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public void valueBound(HttpSessionBindingEvent hsbe) {
            System.out.println("valueBound name: "+hsbe.getName());
        }
    
        public void valueUnbound(HttpSessionBindingEvent hsbe) {
            System.out.println("valueUnbound name: "+hsbe.getName());
        }
        
    }

    Session数据的钝化与活化:

      由于session中保存大量访问网站相关的重要信息,因此过多的session数据就会服务器性能的下降,占用过多的内存。因此类似数据库对象的持久化,web容器也会把不常使用的session数据持久化到本地文件或者数据中。这些都是有web容器自己完成,不需要用户设定。

      不用的session数据序列化到本地文件中的过程,就是钝化;

      当再次访问需要到该session的内容时,就会读取本地文件,再次放入内存中,这个过程就是活化。

      类似的,只要实现HttpSeesionActivationListener接口就是实现钝化与活化事件的监听:

    public class User implements HttpSessionBindingListener,
    HttpSessionActivationListener,Serializable{
    
        private String username;
        private String password;
        
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public void valueBound(HttpSessionBindingEvent hsbe) {
            System.out.println("valueBound name: "+hsbe.getName());
        }
    
        public void valueUnbound(HttpSessionBindingEvent hsbe) {
            System.out.println("valueUnbound name: "+hsbe.getName());
        }
    
        public void sessionDidActivate(HttpSessionEvent hsbe) {
            System.out.println("sessionDidActivate name: "+hsbe.getSource());
        }
    
        public void sessionWillPassivate(HttpSessionEvent hsbe) {
            System.out.println("sessionWillPassivate name: "+hsbe.getSource());
        }
        
    }

    2.1、HttpSessionBindingListener接口

      实现了HttpSessionBindingListener接口的JavaBean对象可以感知自己被绑定到Session中和 Session中删除的事件
      当对象被绑定到HttpSession对象中时,web服务器调用该对象的void valueBound(HttpSessionBindingEvent event)方法
      当对象从HttpSession对象中解除绑定时,web服务器调用该对象的void valueUnbound(HttpSessionBindingEvent event)方法

    范例:

    package com.domain;
    
    import javax.servlet.http.HttpSessionBindingEvent;
    import javax.servlet.http.HttpSessionBindingListener;
    
    /**
    * @ClassName: JavaBeanDemo1
    * @Description: 
    *   实现了HttpSessionBindingListener接口的 JavaBean对象可以感知自己被绑定到 Session中和从Session中删除的事件
        当对象被绑定到 HttpSession 对象中时,web 服务器调用该对象的  void valueBound(HttpSessionBindingEvent event) 方法
        当对象从 HttpSession 对象中解除绑定时,web 服务器调用该对象的 void valueUnbound(HttpSessionBindingEvent event)方法
    
    * @author: hdb
    * @date: 2017-12-11 20:14:54
    *
    */ 
    public class JavaBeanDemo1 implements HttpSessionBindingListener {
    
        private String name;
        
        @Override
        public void valueBound(HttpSessionBindingEvent event) {
            System.out.println(name+"被加到session中了");
        }
    
        @Override
        public void valueUnbound(HttpSessionBindingEvent event) {
            System.out.println(name+"被session踢出来了");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public JavaBeanDemo1(String name) {
            this.name = name;
        }
    }

      上述的JavaBeanDemo1这个javabean实现了HttpSessionBindingListener接口,那么这个JavaBean对象可以感知自己被绑定到Session中和从Session中删除的这两个操作,测试代码如下:

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@page import=" com.domain.JavaBeanDemo1"%>
    <!DOCTYPE HTML>
    <html>
      <head>
        <title></title>
      </head>
      
      <body>
        <% 
            //将javabean对象绑定到Session中
            session.setAttribute("bean",new JavaBeanDemo1("hdb"));
            //从Session中删除javabean对象
            session.removeAttribute("bean");
        %>
      </body>
    </html>

      运行结果如下:

      

    2.2、HttpSessionActivationListener接口

      实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化(反序列化)和钝化(序列化)的事件
      当绑定到HttpSession对象中的javabean对象将要随HttpSession对象被钝化(序列化)之前,web服务器调用该javabean对象的void sessionWillPassivate(HttpSessionEvent event) 方法。这样javabean对象就可以知道自己将要和HttpSession对象一起被序列化(钝化)到硬盘中.
      当绑定到HttpSession对象中的javabean对象将要随HttpSession对象被活化(反序列化)之后,web服务器调用该javabean对象的void sessionDidActive(HttpSessionEvent event)方法。这样javabean对象就可以知道自己将要和 HttpSession对象一起被反序列化(活化)回到内存中

    范例:

    package com.domain;
    
    import java.io.Serializable;
    
    import javax.servlet.http.HttpSessionActivationListener;
    import javax.servlet.http.HttpSessionEvent;
    
    /**
    * @ClassName: JavaBeanDemo2
    * @Description: 
        实现了HttpSessionActivationListener接口的 JavaBean 对象可以感知自己被活化和钝化的事件
        活化:javabean对象和Session一起被反序列化(活化)到内存中。
        钝化:javabean对象存在Session中,当服务器把session序列化到硬盘上时,如果Session中的javabean对象实现了Serializable接口
        那么服务器会把session中的javabean对象一起序列化到硬盘上,javabean对象和Session一起被序列化到硬盘中的这个操作称之为钝化
        如果Session中的javabean对象没有实现Serializable接口,那么服务器会先把Session中没有实现Serializable接口的javabean对象移除
        然后再把Session序列化(钝化)到硬盘中
        当绑定到 HttpSession对象中的javabean对象将要随 HttpSession对象被钝化之前,
        web服务器调用该javabean对象对象的 void sessionWillPassivate(HttpSessionEvent event)方法
        这样javabean对象就可以知道自己将要和 HttpSession对象一起被序列化(钝化)到硬盘中
        当绑定到HttpSession对象中的javabean对象将要随 HttpSession对象被活化之后,
        web服务器调用该javabean对象的 void sessionDidActive(HttpSessionEvent event)方法
        这样javabean对象就可以知道自己将要和 HttpSession对象一起被反序列化(活化)回到内存中
    * @author: hdb
    * @date: 2017-12-11 20:22:35
    *
    */ 
    public class JavaBeanDemo2 implements HttpSessionActivationListener,
            Serializable {
    
         
        private static final long serialVersionUID = 7589841135210272124L;
        private String name;
        
        @Override
        public void sessionWillPassivate(HttpSessionEvent se) {
            
            System.out.println(name+"和session一起被序列化(钝化)到硬盘了,session的id是:"+se.getSession().getId());
        }
    
        @Override
        public void sessionDidActivate(HttpSessionEvent se) {
            System.out.println(name+"和session一起从硬盘反序列化(活化)回到内存了,session的id是:"+se.getSession().getId());
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public JavaBeanDemo2(String name) {
            this.name = name;
        }
    }

      为了观察绑定到HttpSession对象中的javabean对象随HttpSession对象一起被钝化到硬盘上和从硬盘上重新活化回到内存中的的过程,我们需要借助tomcat服务器帮助我们完成HttpSession对象的钝化和活化过程,具体做法如下:

      在WebRootMETA-INF文件夹下创建一个context.xml文件,如下所示:

      

      context.xml文件的内容如下:

     <Context>
         <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
         <Store className="org.apache.catalina.session.FileStore" directory="gacl"/>
         </Manager>
     </Context>

      在context.xml文件文件中配置了1分钟之后就将HttpSession对象钝化到本地硬盘的一个gacl文件夹中

      jsp测试代码如下:

    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@page import="com.domain.JavaBeanDemo2"%>
    <!DOCTYPE HTML>
    <html>
      <head>
        <title></title>
      </head>
      
      <body>
          一访问JSP页面,HttpSession就创建了,创建好的Session的Id是:${pageContext.session.id}
           <hr/>
       <% 
            session.setAttribute("bean",new JavaBeanDemo2("hdb"));
        %>
      </body>
    </html>

      访问这个jsp页面,服务器就会马上创建一个HttpSession对象,然后将实现了HttpSessionActivationListener接口的JavaBean对象绑定到session对象中,这个jsp页面在等待1分钟之后没有人再次访问,那么服务器就会自动将这个HttpSession对象钝化(序列化)到硬盘上,

      

      我们可以在tomcat服务器的workCatalinalocalhostJavaWeb_Listener_20140908gacl文件夹下找到序列化到本地存储的session,如下图所示:

      

      当再次访问这个Jsp页面时,服务器又会自动将已经钝化(序列化)到硬盘上HttpSession对象重新活化(反序列化)回到内存中。运行结果如下:

      

      JavaWeb开发技术中的监听器技术的内容就这么多了,在平时的工作中,监听器技术在JavaWeb项目开发中用得是比较多,因此必须掌握这门技术。

  • 相关阅读:
    JSP实验四
    servlet应用
    SWPU电子邮件页面
    Java Web基础
    软件工程课程总结
    Beta-星期五
    个人作业-Alpha项目测试
    第三次作业
    学期总结-西北师范大学-18计师班助教牛百泉
    Java学习计划
  • 原文地址:https://www.cnblogs.com/huangdabing/p/9219420.html
Copyright © 2011-2022 走看看