Listener--------监听器

Listener:监听器的原理和实例

八大监听器:
      ServletRequest对象:
         生命周期监听:ServletRequestListener
         属性监听:ServletRequestAttributeListener
      HttpSession对象:
         生命周期监听:HttpSessionListener
         属性监听:HttpSessionAttributeListener
         对象绑定监听1:HttpSessionBindingListener
         对象钝化活化监听2:HttpSessionActivationListener
      ServletContext对象:
         生命周期监听:ServletContextListener
         属性监听:ServletContextAttributeListener


一、监听对象,主要监听servlet的三大域对象request,session,application(ServletRequest,HttpSession,ServletContext)

    监听内容:
        A:监听域对象的创建和销毁,也就是生命周期的监听
        B:监听域对象的属性添加,移除和更改
        C:监听被加入域对象中的对象

二、三大域对象的生命周期
    ServletRequest 什么时候被创建:
        A: 请求一个jsp页面时,tomcat 翻译jsp页面为一个servlet类,执行servlet 类中的service()方法是,tomcat帮我们创建了9大内置对象,其中就有request。
        B: 请求一个servlet时,doGet() 或者是doPost()方法,tomcat 自动也会帮我们创建request,
   ServletRequest 什么时候被销毁:
        执行完service()方法或者 doGet()方法,doPost()方法即销毁

    HttSesssion 什么时候被创建:
        Request.getSession():  先判断是否存在session对象,如果存在就直接返回,如果不存在就帮我们创建一个session,然后返回。
    HttSesssion 什么时候被销毁:
       是根据cookie的生命周期来判断的,如果cookie是浏览器生命周期的话,那么浏览器关闭后,session即销毁;如果cookie是保存到文件中的,那么就具体情况,具体对待。
 
    ServletContext 什么时候被创建:
       Tomcat启动项目,即创建了application对象。
    ServletContext 什么时候被销毁:
       Tomcat关闭项目,即销毁了application对象。

三、【实例】
   创建一个监听器
  A:选择一个接口,根据监听对象,监听内容的不同选择不同的接口
  B:在web.xml中添加监听器的配置信息
      <listener>
          <listener-class>监听器文件的相对路径</listener-class>
      </listener>


监听器: HttpSessionAttributeListener
    【原理】
    HttpSessionAttributeListener是对SessionAttribute的监听,当在会话对象中加入属性,移除属性和替换属性时就会触发HttpSessionAttributeListener监

听器。
    接口HttpSessionAttributeListener有3个方法:
    1、public void attributeAdded(HttpSessionBindingEvent sbe){}:该方法在session添加对象是触发
    2、public void attributeRemoved(HttpSessionBindingEvent sbe){}:该方法是在session移除对象时触发的
    3、public voidattributeReplaced(HttpSessionBindingEvent se):在Session属性被重新设置时

   【实例】统计一下系统的登录人数
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()得到session里的属性名
      if(sbe.getName().equals("user")){
         count++;
         //将count放在全局域对象ServletContext里
         sbe.getSession().getServletContext().setAttribute("count",count);
         System.out.println("现在系统登录的用户人数"+count);
       }
       
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
      //session对象中删除属性时触发此方法
      if(sbe.getName().equals("user")){
           count--;
           sbe.getSession().getServletContext().setAttribute("count",count);
           System.out.println("现在系统登录的用户人数"+count);
       }
    }

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

猜你喜欢

转载自www.cnblogs.com/fbbg/p/11099169.html