Listener--HttpSessionListener、ServletRequestListener接口的使用详解

HttpSessionListner

HttpSessionListner监听的是HttpSession域对象的创建与销毁

  • HttpSession对象的生命周期:
    何时创建:第一次调用request.getSession时创建
    何时销毁:服务器关闭销毁、session过期、手动销毁

HttpSessionListener的方法:
Listener--HttpSessionListener、ServletRequestListener接口的使用详解_第1张图片

实例:使用HttpSessionListener统计网站在线人数

监听器:

package pers.zhang.listener; /**
 * @author zhang
 * @date 2019/9/19 - 23:09
 */

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

public class MySessionListener implements HttpSessionListener{
    //计数器
    static int count = 0;
    public synchronized void sessionCreated(HttpSessionEvent se) {
        //session创建,计数器+1
        count++;
        //存入ServletContext域中
        se.getSession().getServletContext().setAttribute("count",count);
        System.out.println("增加一人,现有" + count + "人");
    }

    public synchronized void sessionDestroyed(HttpSessionEvent se) {
        //session销毁,计数器-1
        count--;
        //存入ServletContext域中
        se.getSession().getServletContext().setAttribute("count",count);
        System.out.println();
        System.out.println("减少一人,现有" + count + "人");
    }


}

注册:

    <listener>
        <listener-class>pers.zhang.listener.MySessionListenerlistener-class>
    listener>

启动服务器,访问两次,控制台输出:

增加一人,现有1人
增加一人,现有2

ServletRequestListener

ServletRequestListener监听的是ServletRequest域的创建与销毁。

  • ServletRequest的生命周期
    创建:每一请求都会创建request
    销毁:请求结束

  • ServletRequestListener的方法
    Listener--HttpSessionListener、ServletRequestListener接口的使用详解_第2张图片

实例:统计网站访问次数

监听器:

package pers.zhang.listener;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import java.util.Date;

/**
 * @author zhang
 * @date 2019/9/20 - 0:07
 */



public class MyServletRequestListener implements ServletRequestListener {

    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        System.out.println("requestDestroyed" + "," + new Date());
        System.out.println("当前訪问次数:" + servletRequestEvent.getServletContext().getAttribute("count"));
    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        System.out.println("requestInitialized" + "," + new Date());
        Object count = servletRequestEvent.getServletContext().getAttribute("count");
        Integer cInteger = 0;
        if (count != null) {
            cInteger = Integer.valueOf(count.toString());
        }else{
            count = 0;
        }
        System.out.println("历史訪问次数::" + count);
        cInteger++;
        servletRequestEvent.getServletContext().setAttribute("count", cInteger);
    }
}

注册:

	<listener>
        <listener-class>pers.zhang.listener.MyServletRequestListenerlistener-class>
    listener>

启动服务器并访问,控制台输出:

requestInitialized,Fri Sep 20 00:19:00 CST 2019
历史訪问次数::0
requestDestroyed,Fri Sep 20 00:19:01 CST 2019
当前訪问次数:1

再次访问,控制台输出:

requestInitialized,Fri Sep 20 00:21:31 CST 2019
历史訪问次数::1
requestDestroyed,Fri Sep 20 00:21:31 CST 2019
当前訪问次数:2

你可能感兴趣的:(Listener,web)