HttpSessionListener监听器和HttpSessionAttributeListener监听器

2023-12-13 22:04:52
1.作用:监听Session创建或销毁,即生命周期监听
2.相关方法:
//void sessionCreated(HttpSessionEvent se):创建session时调用
//void sessionDestroyed(HttpSessionEvent se):销毁sessio时调用
3.使用场景:

和前面的ServletContextListener等一样,可以用于监控用户上线和离线

4.代码
HttpSessionListener监听器
package com.listener;

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

public class hspHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        //当session被创建时,设置一个统一的生命周期
        session.setMaxInactiveInterval(30);
        System.out.println("hspHttpSessionListener 监听到session创建="+
                session.getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println("hspHttpSessionListener 监听到session销毁="+
                session.getId());
    }
}

实现类

package com.servlet;

import javax.servlet.ServletContext;
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;

@WebServlet(name = "HiServlet",urlPatterns = "/HiServlet")
public class HiServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	  //获取session对象
        HttpSession session = request.getSession();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

HttpSessionAttributeListener监听器

1.作用:监听Session属性的变化,使用少
2.相关方法:
//void attributeAdded(ServletRequestAttritubeEvent srae):添加属性时
//void attributeReplaced(ServletRequestAttritubeEvent srae):替换属性时
//void attributeRemoved(ServletRequestAttritubeEvent srae):移除属性时
3.代码
监听器
package com.listener;

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

public class hspHttpServletAttributeListener implements HttpSessionAttributeListener {
    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        System.out.println("hspHttpServletAttributeListener 监听到session添加属性="+
                se.getName()+"="+se.getValue());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        System.out.println("hspHttpServletAttributeListener 监听到session删除属性="+
                se.getName());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        System.out.println("hspHttpServletAttributeListener 监听到session修改属性="+
                se.getName()+"="+se.getValue());
    }
}

实现类

package com.servlet;

import javax.servlet.ServletContext;
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;

@WebServlet(name = "HiServlet",urlPatterns = "/HiServlet")
public class HiServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //获取session对象
        HttpSession session = request.getSession();
        //添加数据
        session.setAttribute("age",100);
        session.setAttribute("age",40);
        session.removeAttribute("age");
        System.out.println("HiServlet操作完毕...");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

文章来源:https://blog.csdn.net/weixin_74484843/article/details/134912166
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。