美文网首页Java
域对象中属性变更及感知session绑定的事件监听器

域对象中属性变更及感知session绑定的事件监听器

作者: 考拉考拉啊 | 来源:发表于2020-04-22 10:23 被阅读0次

    域对象中属性的变更的时间监听器就是用来监听ServletContext,HttpSession,HttpServletRequest这三个对象中的属性变更信息事件的监听器。这三个监听器接口分别是ServletContextAttributeListener,

    HttpSessionAttributeListener

    和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

    1.1 attributeAdded方法

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

    各个域属性监听器中的完整语法定义为:

    public void attributeAdded(ServletContextAttributeEvent scae)

    public void attributeReplaced(HttpSessionBindingEvent  hsbe)

    public void attributeRmoved(ServletRequestAttributeEvent srae)

    1.2 attributeRemoved方法

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

    public void attributeRemoved(ServletContextAttributeEvent scae)

    public void attributeRemoved (HttpSessionBindingEvent  hsbe)

    public void attributeRemoved (ServletRequestAttributeEvent srae)

    1.3 attributeReplaced方法

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

    public void attributeReplaced(ServletContextAttributeEvent scae)

    public void attributeReplaced (HttpSessionBindingEvent  hsbe)

    public void attributeReplaced (ServletRequestAttributeEvent srae)

    2.域对象中属性变更的监听器xxxAttributeListener

    ①监听ServletContext,HttpSession,ServletRequest中添加属性,替换属性,移除属性的事件监听器。

    ②以ServletRequestArrributeListener为例,

    //添加属性时被调用

    @Overridepublic void attributeAdded(ServletRequestAttributeEvent arg0) {}//移除属性时被调用

    @Overridepublic void attributeRemoved(ServletRequestAttributeEvent arg0) {}//替换属性时被调用

    @Overridepublic void attributeReplaced(ServletRequestAttributeEvent arg0) {}

    ③这三个监听器ServletContextAttributeListener, ServletRequestAttributeListener, HttpSessionAttributeListener用的都比较少

    ④使用API中的getName()和getValue()方法可以知道修改的属性名和属性值是什么,例如:

    public void attributeAdded(ServletRequestAttributeEvent arg0) {

        System.out.println("向request中添加了一个属性:"+arg0.getName()+":"+arg0.getValue());

    }

    3.感知session绑定的事件监听器

    保存在Session域中的对象可以有多种状态:绑定(session.setAttribute("bean",Object))到Session中;从

    Session域中解除(session.removeAttribute("bean"))绑定;随Session对象持久化到一个存储设备中;随Session对象从一个存储设备中恢复。

    Servlet 规范中定义了两个特殊的监听器接口"HttpSessionBindingListener和HttpSessionActivationListener"来帮助JavaBean

    对象了解自己在Session域中的这些状态: ,实现这两个接口的类不需要 web.xml 文件中进行注册。

    3.1 HttpSessionBindingListener

    实现了HttpSessionBindingListener接口的JavaBean对象可以感知自己被绑定到Session中和

    Session中删除的事件。当对象被绑定到HttpSession对象中时,web服务器调用该对象的void

    valueBound(HttpSessionBindingEvent

    event)方法;当对象从HttpSession对象中解除绑定时,web服务器调用该对象的void

    valueUnbound(HttpSessionBindingEvent event)方法。例如:

    Customer.java

    package com.javaweb.Listener;

    import javax.servlet.http.HttpSessionBindingEvent;

    import javax.servlet.http.HttpSessionBindingListener;

    public class Customer implements HttpSessionBindingListener {

    @Override

    public void valueBound(HttpSessionBindingEvent arg0) {

    System.out.println("绑定到session");

    Object value=arg0.getValue();

    System.out.println(this==value);

    System.out.println(arg0.getName());

    }

    @Override

    public void valueUnbound(HttpSessionBindingEvent arg0) {

    System.out.println("从session中解除绑定");

    }

    }

    HttpSessionBindingListenerTest.jsp

      <body>

      <h4>HttpSessionBindingListener</h4>

        <%

        Customer customer=new Customer();

        session.setAttribute("customer",customer);

        System.out.println("-------------------");

        session.removeAttribute("customer");

        %>

      </body>

     运行后输出:

    3.2HttpSessionActivationListener

    实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化(反序列化)和钝化(序列化)的事件。

    活化:从磁盘中读取session对象

    钝化:向磁盘中写入session对象

    session对象存储在Tomcat服务器的work\Catalina\localhost\contextPath目录下。SESSION.SER

    java中的序列化和反序列化概念:

    把对象转换为字节序列的过程称为对象的序列化,把字节序列恢复为对象的过程称为对象的反序列化。对象的序列化主要有两种用途:

    1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;

    2) 在网络上传送对象的字节序列。

    在很多应用中,需要对某些对象进行序列化,让它们离开内存空间,入住物理硬盘,以便长期保存。比如最常见的是Web服务器中的Session对象,当有

    10万用户并发访问,就有可能出现10万个Session对象,内存可能吃不消,于是Web容器就会把一些seesion先序列化到硬盘中,等要用了,再把保存在硬盘中的对象还原到内存中。

    当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把这个Java对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为Java对象。

    当绑定到HttpSession对象中的javabean对象将要随HttpSession对象被钝化(序列化)之前,web服务器调用该javabean对象的void

    sessionWillPassivate(HttpSessionEvent event)

    方法。这样javabean对象就可以知道自己将要和HttpSession对象一起被序列化(钝化)到硬盘中。

    当绑定到HttpSession对象中的javabean对象将要随HttpSession对象被活化(反序列化)之后,web服务器调用该javabean对象的void

    sessionDidActive(HttpSessionEvent event)方法。这样javabean对象就可以知道自己将要和

    HttpSession对象一起被反序列化(活化)回到内存中。

    监听域对象自身创建和销毁的监听器:ServletContextListener,HttpSessionListener,ServletRequestListener

    域对象中属性变更的监听器:ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener

    监听绑定到HttpSession域中的某个对象的状态的事件监听器:HttpSessionBindingListener,HttpSessionActivationListener

    wx搜索“程序员考拉”,专注java领域,一个伴你成长的公众号!

    相关文章

      网友评论

        本文标题:域对象中属性变更及感知session绑定的事件监听器

        本文链接:https://www.haomeiwen.com/subject/sikuihtx.html