Listener listener of Servlet

Source: Internet
Author: User

Listener listener of Servlet


The Servlet2.5 specification has eight Listener interfaces and six Event types.

ServletContextListenerInterface

[Interface method]ContextInitialized() AndContextDestroyed()

[Receive events]ServletContextEvent

[Trigger scenario] contextInitialized () is called when the Container loads a Web application (for example, after the Container is started), and The contextDestroyed () method is called when the Container removes the Web application.


ServletContextAttributeListener

[Interface method]AttributeAdded(),AttributeReplaced(),AttributeRemoved()

[Receive events]ServletContextAttributeEvent

[Trigger scenario] if an object is added as the attribute of the application (ServletContext) object, attributeAdded () is called. Similarly, attributeReplaced () is called when attributes are replaced or removed () attributeRemoved ().


HttpSessionListener

[Interface method]SessionCreated() AndSessionDestroyed()

[Receive events]HttpSessionEvent

[Trigger scenario] When a session (HttpSession) object is created or eliminated, the two methods are called respectively.


HttpSessionAttributeListener

[Interface method]AttributeAdded(),AttributeReplaced(),AttributeRemoved()

[Receive events]HttpSessionBindingEvent

[Trigger scenario] if an object is added as the attribute of the session (HttpSession) object, attributeAdded () is called. Similarly, attributeReplaced () is called when the attribute is replaced or removed () attributeRemoved ().


HttpSessionActivationListener

[Interface method]SessionDidActivate() AndSessionWillPassivate()

[Receive events]HttpSessionEvent

[Trigger scenario] Activate and Passivate are actions used to replace objects, when the session object must be temporarily stored to the hard disk or other storage devices (serialized through the object) for reasons such as resource utilization or load balancing, the action is called Passivate, the session object on the hard disk or storage device is called Activate when the JVM is reloaded. It is easy to understand that sessionDidActivate () and sessionWillPassivate () are called after Activeate and before Passivate respectively.


ServletRequestListener

[Interface method]RequestInitialized() AndRequestDestroyed()

[Receive events]RequestEvent

[Trigger scenario] the two methods are called when the request (HttpServletRequest) object is created or eliminated.


ServletRequestAttributeListener

[Interface method]AttributeAdded(),AttributeReplaced(),AttributeRemoved()

[Receive events]HttpSessionBindingEvent

[Trigger scenario] if an object is added as an attribute of the request (HttpServletRequest) object, attributeAdded () is called. Similarly, attributeReplaced () is called when attributes are replaced or removed () attributeRemoved ().


HttpSessionBindingListener

[Interface method]ValueBound() AndValueUnbound()

[Receive events]HttpSessionBindingEvent

[Trigger scenario] class for implementing the HttpSessionBindingListener interface. For example, if it is added to the attribute of the session (HttpSession) object, valueBound () is called. If it is removed from the session (HttpSession) when you remove an object from its attributes, the system calls valueUnbound () to implement the HttpSessionBindingListener interface. set in xml.

 

In addition to HttpSessionBindingListener, you must register with the container in web. xml before the container can call the corresponding category when the corresponding event occurs, as shown in figure

< listener>       < listener-class >             demo.servlet.listener.CustomServletContextListener        < /listener-class >< /listener>

 

Public class ListenerTest implements HttpSessionListener {Log log = LogFactorygetLog (getClass (); public void sessionCreated (HttpSessionEvent event) {httpSession session = event. getSession (); log.info ("Create a session with the ID of" + session. getId ();} public void sessionDestroyed (HttpSessionEvent event) {httpSession session = event. getSession (); log.info ("destroy a session, ID is" + session. getId ());}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.