Listener监听器----HttpSession对象的生命周期监听器

一、HttpSession对象的生命周期监听器

HttpSessionListener接口定义了HttpSession对象生命周期的监听行为。

void sessionCreated(HttpSessionEvent se)

        HttpSession对象创建后会触发该监听器方法,并将已创建HttpSession对象传递到该方法中

void sessionDestoryed(HttpSessionEvent se)

        HttpSession对象在销毁之前会触发该监听方法,并将要销毁的HttpSession对象传递到该方法中

二、创建能够监听HttpSession生命周期的监听器 

 web.xml

    
        cn.itcast.listener.HttpSessiontLifecycleListener
    

 实现类:

package cn.itcast.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class HttpSessiontLifecycleListener implements HttpSessionListener {
    /*
     监听HttpSession 对象创建的监听方法
     */
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("sessionCreated");
        System.out.println(se.getSession());
    }
    /*
     监听HttpSession 对象创建的监听方法
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("sessionDestroyed");
        System.out.println(se.getSession());
    }
}

三、HttpSession对象的属性操作监听器

HttpSessionAttributeListener接口定义了对于HttpSession对象属性操作的监听行为。

void attributeAdded(HttpSessionBindingEvent se)

        HttpSession对象中添加属性是会触发该监听方法,并将HttpSession对象传递到该方法中,触发事件的方法HttpSession.setAttribute("key","value")

void attributeRemove(HttpSessionBindingEvent se)

        HttpSession对象中添加属性是会触发该监听方法,并将HttpSession对象传递到该方法中,触发事件的方法HttpSession.removeAttribute("key","value")

void attributeReplaced(HttpSessionBindingEvent se)

        HttpSession对象中添加属性是会触发该监听方法,并将HttpSession对象传递到该方法中,触发事件的方法HttpSession.setAttribute("key","value")

实例代码:

servlet:

package cn.itcast.listener;

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;

/**
 * 测试HttpSession属性监听器
 */
@WebServlet("/sessionAttr.do")
public class HttpSessionAttrServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取httpSession对象
        HttpSession httpSession = req.getSession();
        httpSession.setAttribute("key","test");
        httpSession.setAttribute("key","test2");
        httpSession.removeAttribute("key");
    }
}

 监听:

package cn.itcast.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class HttpSessionAttrListener implements HttpSessionAttributeListener {
    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        System.out.println("-------HttpSession---Start attributeAdded----------");
        System.out.println("Name:"+se.getName()+" Value:"+se.getValue());
        System.out.println("-------HttpSession---End attributeAdded----------");
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        System.out.println("-------HttpSession---Start attributeRemoved----------");
        System.out.println("Name:"+se.getName()+" Value:"+se.getValue());
        System.out.println("-------HttpSession---End attributeRemoved----------");
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        System.out.println("-------HttpSession---Start attributeReplaced----------");
        System.out.println("Name:"+se.getName()+" Value:"+se.getValue());
        System.out.println("-------HttpSession---End attributeReplaced----------");
    }
}

输出结果:

-------HttpSession---Start attributeAdded----------
Name:key  Value:test
-------HttpSession---End attributeAdded----------

-------HttpSession---Start attributeReplaced----------

Name:key  Value:test
-------HttpSession---End attributeReplaced----------

-------HttpSession---Stary attributeRemoved----------

Name:key  Value:test2

-------HttpSession---End attributeRemoved----------

你可能感兴趣的:(HttpSession,Java,监听器,java,前端,服务器)