JAVA监听器Listener

时间:2023-03-08 16:13:19
JAVA监听器Listener

JAVA监听器Listener
一、 简介
监听器用于对web中内置对象的状态或者属性变化进行监听并做出相应响应的一种Servlet;在内置对象的生命周期中,产生、销毁等状态发生变化时,监听器就会进行监听并做出响应,当内置对象的属性发生增加,删除,修改等变化时,监听器也会做出相应响应。
二、 分类
ServletContext对象监听器
HttpSession对象监听器
ServletRequest对象监听器
三、 如何配置监听器
在web.xml中添加:


<listener>
        <listener-class>com.zsl33.mvc.listener.RequestListener</listener-class>
        <listener-class>com.zsl33.mvc.listener.SessionListener</listener-class>
        <listener-class>com.zsl33.mvc.listener.ApplicationListener</listener-class>
    </listener>

使用注解进行配置:
在监听器类上加上
@WebListener

四、 接口
Session监听器类对Session状态进行监听需要实现javax.servlet.http.HttpSessionListener 接口,对Session属性进行监听则需要实现``` javax.servlet.http.HttpSessionAttributeListener 接口
实例:

@WebListener
public class SessionListener implements HttpSessionListener,HttpSessionAttributeListener{

    @Override
    public void attributeAdded(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("session增加:"+arg0.getSession().getId());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("session删除:"+arg0.getSession().getId());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("session替换:"+arg0.getSession().getId());
    }

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("session创建:"+arg0.getSession().getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("session销毁:"+arg0.getSession().getId());
    }

}

Request监听器类对Request状态进行监听需要实现javax.servlet.ServletRequestListener 接口,对Request属性进行监听需要实现
javax.servlet.ServletRequestAttributeListener 接口
实例:

@WebListener
public class RequestListener implements ServletRequestListener,ServletRequestAttributeListener{

    @Override
    public void requestDestroyed(ServletRequestEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("监听:Request内置对象销毁");
    }

    @Override
    public void requestInitialized(ServletRequestEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("监听:Request内置对象初始化");
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("监听:新增:"+arg0.getName()+":"+arg0.getValue());
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("监听:删除:"+arg0.getName()+":"+arg0.getValue());
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("监听:替换:"+arg0.getName()+":"+arg0.getValue());
    }

}

Application监听器类对Application对象状态进行监听,需要实现javax.servlet.ServletContextListener 接口,对Application对象属性进行监听需要实现
javax.servlet.ServletContextAttributeListener 接口;
实例:

@WebListener
public class ApplicationListener implements ServletContextListener,ServletContextAttributeListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("监听:Application内置对象销毁");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("监听:Application内置对象初始化");

    }

    @Override
    public void attributeAdded(ServletContextAttributeEvent arg0) {
        System.out.println("监听:新增:"+arg0.getName()+":"+arg0.getValue());

    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent arg0) {
        System.out.println("监听:删除:"+arg0.getName()+":"+arg0.getValue());

    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent arg0) {
        System.out.println("监听:替换:"+arg0.getName()+":"+arg0.getValue());

    }

}