Servlet——Listener监听器

Posted tractors

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet——Listener监听器相关的知识,希望对你有一定的参考价值。

一、监听器配置

  1.基于注解配置:@WebListener;//监听器>过滤器>serlvet,配置的时候要注意先后顺序;

  2.基于web.xml配置:

    <listener>
        <listener-class>全类名</listener-class>
    </listener>

二、监听域对象/域对象属性:继承对应的接口

  1.ServletContext(application)

    (1)监听生命周期:ServletContextListener;

      1)创建:void contextInitialized(ServletContextEvent sce);

      2)销毁:void contextDestroyed(ServletContextEvent sce);

    (2)监听属性:ServletContextAttributeListener;

      1)添加属性:void attributeAdded(ServletContextAttributeEvent event);

      2)替换属性:void attributeReplaced(ServletContextAttributeEvent event);

      3)移除属性:void attributeRemoved(ServletContextAttributeEvent event);

  2.HttpSession:

    (1)监听生命周期:HttpSessionListener;

      1)创建:void sessionCreated(HttpSessionEvent se);

      2)销毁:void sessionDestroyed(HttpSessionEvent se);

    (2)监听属性:HttpSessionAttributeListener;

      1)添加属性:void attributeAdded(HttpSessionBindingEvent event);

      2)替换属性:void attributeReplaced(HttpSessionBindingEvent event);

      3)移除属性:void attributeRemoved(HttpSessionBindingEvent event);

  3.ServletRequest:

    (1)监听生命周期:ServletRequestListener;

      1)创建:void requestInitialized(ServletRequestEvent sre);

      2)销毁:void requestDestroyed(ServletRequestEvent sre);

    (2)监听属性:ServletRequestAttributeListener;

      1)添加属性:void attributeAdded(ServletRequestAttributeEvent srae);

      2)替换属性:void attributeReplaced(ServletRequestAttributeEvent srae);

      3)移除属性:void attributeRemoved(ServletRequestAttributeEvent srae);

@WebListener
public class ListenerTest implements ServletRequestListener 
    @Override
    public void requestDestroyed(ServletRequestEvent sre) 
        System.out.println("requestDestroyed......");
    
    @Override
    public void requestInitialized(ServletRequestEvent sre) 
        System.out.println("requestInitialized......");
    
@WebListener
public class ListenerTest implements ServletRequestAttributeListener 
    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) 
        System.out.println("attributeAdded......");
    

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) 
        System.out.println("attributeRemoved......");
    
    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) 
        System.out.println("attributeReplaced......");
    

 

 

以上是关于Servlet——Listener监听器的主要内容,如果未能解决你的问题,请参考以下文章

Servlet3.0的注解自定义原生Listener监听器实战

JavaWeb开发——servlet监听器 listener

java_day29_Servlet三大组件之监听器listener

Servlet监听器(Listener)

2020.05.03 Listener监听器

servlet技术中的listener有啥具体作用