IT Band of Brothers JavaWeb tutorial listener 4

Perception listener Session binding events

Session object stored in the domain can have multiple states: Binding (session.setAttribute ( "bean", Object) into the Session; release (session.removerAttribute ( "bean") from the Session binding domain; Session object with a persistent storage device; Session object with recovery from a storage device.

Servlet specification defines two special listener interface HttpSessionBindingListener and HttpSessionActivationListener JavaBean objects to help them understand their own state in the Session domain class that implements both interfaces need not be registered in the web.xml file.

HttpSessionBindingListener Interface

JavaBean to achieve the target HttpSessionBindingListener interface may perceive themselves to be bound to the Session and Session event deleted.

When an object is bound to the HttpSession object, web server calls the object valueBound (HttpSession Event event) method.

When the object is released from the binding HttpSession object, web server calls the object valueUnbound (HttpSessionBindingEvent event) method.

 

HttpSessionBindingListener listener examples:

● prepared listener listening Session state objects in the domain, as follows:

package com.xdl.listener;

import javax.servlet.http.HttpSessionBindingEvent;

import javax.servlet.http.HttpSessionBindingListener;

/**

 * MySessionBindingListener class implements the interface HttpSessionBindingListener

 */

public class MySessionBindingListener implements HttpSessionBindingListener{

    private String name;

    public MySessionBindingListener(String name) {

        this.name = name;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public void valueBound(HttpSessionBindingEvent event) {

        System.out.println (name + "is added in the session");

    }

    @Override

    public void valueUnbound(HttpSessionBindingEvent event) {

        System.out.println (name + "is out of the session");

    }

}

● write SessionBindingListenerTest.jsp test page.

<%@page import="com.xdl.listener.MySessionBindingListener"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

     pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<Title> Band of Brothers IT Education </ title>

</head>

<body>

    <%

        // will MySessionBindingListener object is bound to the Session

        session.setAttribute("bean",new MySessionBindingListener("三十画生"));

        // delete the object from the Session MySessionBindingListener

        session.removeAttribute("bean");

    %>

   </body>

</html>

Open Tomcat server, the results shown in Figure 14.

6bbc6d8ee6174146ba170b2b7f2b9820.png

FIG 14 MySessionBindingListener output information in the console

 

●  HttpSessionActivationListener接口

JavaBean to achieve the target HttpSessionActivationListener interface may perceive that they have been activated (deserialized) and passive (serialization) event.

Before void sessionWillPassivate bound to the object javabean HttpSession object to be passivated with HttpSession object (serialized), web server calls the object javabean (HttpSessionEvent event) method. Such objects can javabean will know and HttpSession object is serialized together (passivation) to the hard disk.

When bound to the object javabean HttpSession object to be activated with HttpSession object (deserialization) after, web server calls the void sessionDidActive javabean object (HttpSessionEvent event) method. Such objects can javabean know they were going to and HttpSession objects are deserialized together (activated) back to memory.

 

HttpSessionActivationListener listener examples:

● prepared listener listening session object in the passivation and activation event, as follows:

package com.xdl.listener;

import java.io.Serializable;

import javax.servlet.http.HttpSessionActivationListener;

import javax.servlet.http.HttpSessionEvent;

/**

* MySessionActivationListener class implements

* HttpSessionActivationListener and Serializable interface

*/

public class MySessionActivationListener

    implements HttpSessionActivationListener,Serializable {

    private static final long serialVersionUID = 1L;

    private String name;

    public MySessionActivationListener(String name) {

         this.name = name;

    }

    public String getName() {

         return name;

    }

    public void setName(String name) {

         this.name = name;

    }

    @Override

    public void sessionDidActivate(HttpSessionEvent se) {

         System.out.println(name

              + "And the session is a sequence of the hard disk together, session id's is:"

              + se.getSession().getId());

    }

    @Override

    public void sessionWillPassivate(HttpSessionEvent se) {

         System.out.println(name

              + "And a session with the hard disk back to memory the de-serialization, session's id is:"

              + se.getSession().getId());

    }

}

● the observed objects bound to MySessionActivationListener HttpSession object together with the HttpSession object is passivated to the hard disk from the hard disk and re-activation of the process back to memory, we need to help us help tomcat server HttpSession object and passivation activation process, specifically, the following:

In WebContent \ META-INF folder, create a context.xml file shown in Figure 15.

b7b1f48d42464a8c8369ba3d8426ed15.png

15 Create a context.xml file

 

Context.xml file contents are as follows:

<?xml version="1.0" encoding="UTF-8"?>

<Context>

     <Manager className="org.apache.catalina.session.PersistentManager"

     maxIdleSwap="1">

         <Store className="org.apache.catalina.session.FileStore"

             directory= "xdl"/>

    </Manager>

</Context>

After 1 minute context.xml configuration file will HttpSession object file to the local hard disk of a passivation xdl folder.

 

● write SessionActivationListenerTest.jsp test page

<%@page import="com.xdl.listener.MySessionActivationListener"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<Title> Band of Brothers IT Education </ title>

</head>

<body>

A JSP page is accessed, HttpSession is created, the created Session of Id is: $ {. PageContext session.id}

    <hr>

    <%

        session.setAttribute("bean", new MySessionActivationListener("三十画生"));

    %>

   </body>

</html>

Access the JSP page, the server will immediately create a HttpSession object, and then implement the JavaBean objects HttpSessionActivationListener interface bound to the session object, no one visits this jsp page again after waiting one minute, then the server will automatically passivation HttpSession object (serialized) to the hard disk.

d1d2cd8b7f254036812e44775163440b.png

16 Creating a Session

 

9407e4f7d91a40758d9a9750b50f61f3.png

FIG 17 Session passivated to disk

 

We can work tomcat server \ Catalina \ localhost \ XDL \ xdl folder to find the sequence of locally stored session, shown in Figure 18.

55b532b11f724b08accb2be4b9635968.png

18 

 

When accessing the JSP page again, the server will automatically have been passivated (serialized) to the hard disk HttpSession object re-activation (deserialization) back to memory. The results shown in Figure 19 runs.

fbd8d064db7241df950553d6bca8dc59.png

FIG 19 is passivated Session reactivated

Guess you like

Origin www.cnblogs.com/itxdl/p/10961953.html