Java Web Listener

Source: Internet
Author: User

Java Web Listener
I. Event Source: three major domains!

  • ServletContext
    • Life Cycle listener: ServletContextListener, which has two methods: one called at creation and the other called at destruction;
      • Void contextInitialized (ServletContextEvent sce): When you create a Servletcontext
      • Void contextDestroyed (ServletContextEvent sce): When Servletcontext is destroyed
    • Attribute listening: ServletContextAttributeListener. It has three methods: one called when adding an attribute, one called when replacing the attribute, and the other called when removing the attribute.
      • Void attributeAdded (ServletContextAttributeEvent): when adding an attribute;
      • Void attributeReplaced (ServletContextAttributeEvent event): When the attribute is replaced;
      • Void attributeRemoved (ServletContextAttributeEvent event): When the attribute is removed;
  • HttpSession
    • Lifecycle listener: HttpSessionListener. It has two methods: one called at creation and the other called at destruction;
      • Void sessionCreated (HttpSessionEvent se): When a session is created
      • Void sessionDestroyed (HttpSessionEvent se): When the session is destroyed
    • Property listening: HttpSessioniAttributeListener. It has three methods: one called when adding an attribute, one called when replacing the attribute, and the other called when removing the attribute.
      • Void attributeAdded (HttpSessionBindingEvent event): when adding an attribute;
      • Void attributeReplaced (HttpSessionBindingEvent event): When the attribute is replaced
      • Void attributeRemoved (HttpSessionBindingEvent event): When the attribute is removed
  • ServletRequest
    • Life Cycle listener: ServletRequestListener, which has two methods: called at creation and destroyed;
      • Void requestInitialized (ServletRequestEvent sre): when a request is created
      • Void requestDestroyed (ServletRequestEvent sre): when the request is destroyed
    • Property listening: ServletRequestAttributeListener, which has three methods: one called when adding an attribute, one called when replacing the attribute, and the other called when removing the attribute.
      • Void attributeAdded (ServletRequestAttributeEvent srae): when adding an attribute
      • Void attributeReplaced (ServletRequestAttributeEvent srae): When the attribute is replaced
      • Void attributeRemoved (ServletRequestAttributeEvent srae): When the attribute is removed
2. Complete listener compilation in Java Web:
  • Write a listener class: A listener interface must be implemented;
  • Registration is configured in web. xml to complete registration!
1 package web. listener; 2 3 import javax. servlet. servletContextEvent; 4 import javax. servlet. servletContextListener; 5 import javax. servlet. annotation. webListener; 6 7/** 8 * ServletContext listener 9 * can store some code to be completed when Tomcat is started! 10 **/11 @ WebListener () 12 public class AListener implements ServletContextListener {13 @ Override14 public void contextInitialized (ServletContextEvent sce) {15 System. out. println ("wow, I'm coming"); 16} 17 18 @ Override19 public void contextDestroyed (ServletContextEvent sce) {20 System. out. println ("wow, goodbye"); 21} 22}
Iii. Event objects:
  • ServletContextEvent: ServletContext getServletContext ()
  • HttpSessionEvent: HttpSession getSession ()
  • ServletRequest:
    • ServletContext getServletContext ();
    • ServletReques getServletRequest ();
  • ServletContextAttributeEvent:
    • ServletContext getServletContext ();
    • String getName (): Get the property name
    • Object getValue (): Get the property value
  • HttpSessionBindingEvent: omitted
  • ServletRequestAttributeEvent: omitted
4. listener (all associated with HttpSession)
  • It is used to add to the JavaBean, rather than to the three domains!
  • Both listeners do not need to be registered in web. xml!
HttpSessionBindingListener: add it to the javabean, And the javabean will know whether it is added to the session.

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.