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中,后者创建一次

05-12 13:54