码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet——Listener监听器

时间:2019-07-31 23:46:03      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:init   对象   context   str   move   ace   des   class   https   

一、监听器配置

  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监听器

标签:init   对象   context   str   move   ace   des   class   https   

原文地址:https://www.cnblogs.com/Tractors/p/11279763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!