Listener -------- listeners

Listener: Principle and examples of the listener

eight listener:
      the ServletRequest Object:
         lifecycle listeners: ServletRequestListener
         Properties Listener: ServletRequestAttributeListener
      the HttpSession Object:
         lifecycle listeners: the HttpSessionListener
         Properties Listener: HttpSessionAttributeListener
         objects bound monitor 1: HttpSessionBindingListener
         Object Activation passivation monitor 2: HttpSessionActivationListener
      the ServletContext objects:
         lifecycle listeners: ServletContextListener
         property monitor: ServletContextAttributeListener


a listener objects, mainly listening servlet three domain object request, session, application (ServletRequest, HttpSession, ServletContext)

    monitor content:
        a: listen domain object creation and destruction, that is the life cycle of the monitor
        B: add a listener attribute domain objects, remove, and change
        C: listener objects are added to the domain object

Second, the life cycles of the three domain objects
    ServletRequest What time is created:
        A: request a jsp page, tomcat translation jsp page into a servlet class, perform servlet class service () method, tomcat help us create a nine built objects, among them the request.
        B: request a servlet, doGet () or doPost () method, tomcat will automatically help us create the request,
   when ServletRequest be destroyed:
        executing the service () method or the doGet () method, doPost () method that is destroyed

    HttSesssion What time is created:
        request.getSession (): first determine whether there is a session object, if there is direct return, if it does not exist to help us create a session, then return.
    HttSesssion What time is destroyed:
       it is based on lifetime of the cookie to determine if the cookie is a browser lifecycle, then the browser is closed, session that is destroyed; if the cookie is saved to a file, then specific cases, specific treat.
 
    ServletContext is created when:
       Tomcat start the project, which created the application object.
    When ServletContext is destroyed:
       Tomcat close the project, namely the destruction of the application object.

Third, [Example]
   Create a listener
  A: selecting an interface, according to the intercepted object, different content selection listener interface
  B: add configuration information listener in web.xml
      <listener>
          <listener-class> listener relative path of the file < / listener-class>
      </ listener>


listener: HttpSessionAttributeListener
    [principle]
    HttpSessionAttributeListener is listening to SessionAttribute when adding properties in the session object, remove and replace attribute properties will trigger HttpSessionAttributeListener monitor

hearing.
    There are three methods HttpSessionAttributeListener Interface:
    . 1, attributeAdded public void (HttpSessionBindingEvent SBE) {}: add an object which is triggered session
    2, public void attributeRemoved (HttpSessionBindingEvent SBE) {}: This method is triggered when a session object is removed
    3, public voidattributeReplaced (HttpSessionBindingEvent se) : when the Session property is reset

   [example] statistics about the number of registrants system
package com.aaa.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener()
public class Listener2 implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

      public int count=0;//统计登录的人数

    // Public constructor is required by servlet spec
    public Listener2() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed).
         You can initialize servlet context related data here.
      */
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context
         (the Web application) is undeployed or
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is added to a session.
      */
     //session对象中加入属性了触发此方法
     //sbe.getName () to get the property name in the session
      IF {(sbe.getName () the equals ( "the User").)
         count ++;
         // will count on the global domain objects ServletContext in
         sbe.getSession () getServletContext (. ) .setAttribute ( "COUNT", COUNT);
         System.out.println ( "number of users logged in the system now" + COUNT);
       }
       
    }

    public void attributeRemoved (HttpSessionBindingEvent SBE) {
      / * This Method iS AN attribute Called When
         iS removed . A from the session
      * /
      // this method triggers the session object delete property
      IF {(sbe.getName () the equals ( "User").)
           count--;
           .. sbe.getSession () GetServletContext () the setAttribute ( " COUNT ", COUNT);
           System.out.println ("Now the number of users logged on the system "+ count);
       }
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}

Guess you like

Origin www.cnblogs.com/fbbg/p/11099169.html