HttpSessionActivationListener钝化与活化监听器

在Servlet的学习中,存在Listener这个技术点,本文主要讲述使用HttpSessionActivationListener钝化与活化Session中的实体。


钝化 -- 将实体存储到硬盘中(序列化)

活化 -- 将硬盘中的实体读取到内存中(反序列化)


1、要钝化与活化Session中的实体,首先要在该实体实现HttpSessionActivationListener和Serializable接口。

package com.liweijian.domain;

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
import java.io.Serializable;

/**
 * @Author:Liweijian
 * @Description: 钝化与活化监听器
 * @Date:Create in 15:55 2017/12/14 0014
 */
public class Customer implements HttpSessionActivationListener, Serializable{
    private String id;
    private String name;

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    //钝化 -- 存储到硬盘
    @Override
    public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
        System.out.println("session被钝化了..");
    }

    //活化 -- 读取到内存
    @Override
    public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
        System.out.println("session被活化了..");
    }
}

2、其次,在web/META-INF 目录下新建context.xml文件,修改maxIdleSwap属性的值,该值代表实体多久时间(分钟)不操作被钝化。



    
    
     
        
    


3、创建一个Servlet,将Customer添加到Session中,并等待1分钟后将提示实体被钝化

      如果容器被关闭,也会钝化。

package com.liweijian.web;

import com.liweijian.domain.Customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author:Liweijian
 * @Description: SessionActivationListener测试
 * @Date:Create in 16:08 2017/12/14 0014
 */
@WebServlet(name = "TestSessionActivationServlet")
public class TestSessionActivationServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        Customer customer = new Customer();
        customer.setId("200");
        customer.setName("wangwu");

        session.setAttribute("customer",customer);
    }
}

4、如果再次访问该实体,将会自动被活化。

package com.liweijian.web;

import com.liweijian.domain.Customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author:Liweijian
 * @Description: 活化Customer
 * @Date:Create in 16:19 2017/12/14 0014
 */
@WebServlet(name = "TestSessionActivationServlet2")
public class TestSessionActivationServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
        String name = customer.getName();
        System.out.println(name);
    }
}

备注:如果不实现Serializable接口,则无法序列化,也就无法钝化和活化。

钝化后的文件在apache-tomcat-8.0.9\work\Catalina\localhost目录下。

你可能感兴趣的:(JavaEE)