学习java web中的listener

web.xml里的顺序为:context-param->listener->filter->servlet

监听器是需要新建一个类,然后按监听的对象继承:ServletContext、HttpSession、ServletRequest中的一个


 

监听ServletContext生命周期的Listener

 //ServletContext  lifecycle changes
public interface ServletContextListener extends EventListener {

    //initialized
    public void contextInitialized(ServletContextEvent sce);

 //destroyed
    public void contextDestroyed(ServletContextEvent sce);
}

监听ServletContext属性的Listener

 

public interface ServletContextAttributeListener extends EventListener {

    /**
     * Receives notification that an attribute has been added to the
     * ServletContext.
     *
     * @param event the ServletContextAttributeEvent containing the
     * ServletContext to which the attribute was added, along with the
     * attribute name and value
     */
    public void attributeAdded(ServletContextAttributeEvent event);

    /**
     * Receives notification that an attribute has been removed
     * from the ServletContext.
     *
     * @param event the ServletContextAttributeEvent containing the
     * ServletContext from which the attribute was removed, along with
     * the attribute name and value
     */
    public void attributeRemoved(ServletContextAttributeEvent event);

    /*
     * Receives notification that an attribute has been replaced
     * in the ServletContext.
     *
     * @param event the ServletContextAttributeEvent containing the
     * ServletContext in which the attribute was replaced, along with
     * the attribute name and its old value
     */
    public void attributeReplaced(ServletContextAttributeEvent event);
}

 

 

 

监听HttpSession生命周期的Listener

//about HttpSession lifecycle changes.
public interface HttpSessionListener extends EventListener {
    
    /** 
     * Receives notification that a session has been created.
     *
     * @param se the HttpSessionEvent containing the session
     */
    public void sessionCreated(HttpSessionEvent se);
    
    /** 
     * Receives notification that a session is about to be invalidated.
     *
     * @param se the HttpSessionEvent containing the session
     */
    public void sessionDestroyed(HttpSessionEvent se);
    
}

监听HttpSession属性的Listener

public interface HttpSessionAttributeListener extends EventListener {

    /**
     * Receives notification that an attribute has been added to a
     * session.
     *
     * @param event the HttpSessionBindingEvent containing the session
     * and the name and value of the attribute that was added
     */
    public void attributeAdded(HttpSessionBindingEvent event);

    /**
     * Receives notification that an attribute has been removed from a
     * session.
     *
     * @param event the HttpSessionBindingEvent containing the session
     * and the name and value of the attribute that was removed
     */
    public void attributeRemoved(HttpSessionBindingEvent event);

    /**
     * Receives notification that an attribute has been replaced in a
     * session.
     *
     * @param event the HttpSessionBindingEvent containing the session
     * and the name and (old) value of the attribute that was replaced
     */
    public void attributeReplaced(HttpSessionBindingEvent event);

}

 

监听ServletReques生命周期的tListener

public interface ServletRequestListener extends EventListener {

    /**
     * Receives notification that a ServletRequest is about to go out
     * of scope of the web application.
     *
     * @param sre the ServletRequestEvent containing the ServletRequest
     * and the ServletContext representing the web application
     */
    public void requestDestroyed(ServletRequestEvent sre);

    /**
     * Receives notification that a ServletRequest is about to come
     * into scope of the web application.
     *
     * @param sre the ServletRequestEvent containing the ServletRequest
     * and the ServletContext representing the web application
     */
    public void requestInitialized(ServletRequestEvent sre);
}

监听ServletReques属性的Listener

public interface ServletRequestAttributeListener extends EventListener {

    /**
     * Receives notification that an attribute has been added to the
     * ServletRequest.
     *
     * @param srae the ServletRequestAttributeEvent containing the 
     * ServletRequest and the name and value of the attribute that was
     * added
     */
    public void attributeAdded(ServletRequestAttributeEvent srae);

    /**
     * Receives notification that an attribute has been removed from the
     * ServletRequest.
     *
     * @param srae the ServletRequestAttributeEvent containing the 
     * ServletRequest and the name and value of the attribute that was
     * removed
     */
    public void attributeRemoved(ServletRequestAttributeEvent srae);

    /**
     * Receives notification that an attribute has been replaced on the
     * ServletRequest.
     *
     * @param srae the ServletRequestAttributeEvent containing the 
     * ServletRequest and the name and (old) value of the attribute
     * that was replaced
     */
    public void attributeReplaced(ServletRequestAttributeEvent srae);
}

HttpSessionBindingListener:该接口有两个方法,valueBound和valueUnbound,实现该接口的对象要new以后放在session里。

HttpSessionActivationListener :实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化(反序列化)sessionDidActivate和钝化(序列化)sessionWillPassivate的事件

HttpSessionBindingListener和HttpSessionListener区别是前者要创建对象后放入session中,后者创建一次

转载于:https://www.cnblogs.com/javage/p/9304804.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值