栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

源码解析 -- 什么是监听器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

源码解析 -- 什么是监听器

源码解析 – 什么是监听器 什么是监听器 监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。 场景 监听器可以用来检测网站的在线人数,统计网站的访问量等等! 监听器组件 事件源
  • 被监听对象
事件
  • 事件源做了什么
监听器
  • 监听的对象
注册监听器
  • 将监听器与事件源进行绑定。
代码实现监听
public class PersonEntity {
public PersonListen personListen;

public void say(){
System.out.println(“在说话”);
//时间注册
personListen.listen(new PersonEvent(this));
}

public static void main(String[] args)
{
PersonEntity entity=new PersonEntity();
entity.say();
}
}


public class PersonEvent {
public PersonEntity personEntity;

public PersonEvent(PersonEntity personEntity){
System.out.println(“触发了事件监听”);
this.personEntity=personEntity;
}

}


public interface PersonListen {

public void listen(PersonEvent event);
}

Servle监听器 ServletContext
  • Servlet 容器启动时,会为每个 Web 应用(webapps 下的每个目录都是一个 Web 应用)创建一个唯一的 ServletContext 对象,该对象一般被称为“Servlet 上下文”。

  • ServletContext 对象的生命周期从 Servlet 容器启动时开始,到容器关闭或应用被卸载时结束。

  • ServletContextListener

    • public interface ServletContextListener extends EventListener {
      default void contextInitialized(ServletContextEvent sce) {
      }

    default void contextDestroyed(ServletContextEvent sce) {
    }
    }

  • ServletContextAttributeListener

    • public interface ServletContextAttributeListener extends EventListener {
      default void attributeAdded(ServletContextAttributeEvent scae) {
      }

    default void attributeRemoved(ServletContextAttributeEvent scae) {
    }

    default void attributeReplaced(ServletContextAttributeEvent scae) {
    }
    }

ServletRequest
  • 定义一个Servlet引擎产生的对象,通过这个对象,Servlet可以获得客户端请求的数据。这个对象通过读取请求体的数据提供包括参数的名称、值和属性以及输入流的所有数据。

  • ServletRequestListener

    • public interface ServletRequestListener extends EventListener {
      default void requestDestroyed(ServletRequestEvent sre) {
      }

    default void requestInitialized(ServletRequestEvent sre) {
    }
    }

  • ServletRequestAttributeListener

    • public interface ServletRequestAttributeListener extends EventListener {
      default void attributeAdded(ServletRequestAttributeEvent srae) {
      }

    default void attributeRemoved(ServletRequestAttributeEvent srae) {
    }

    default void attributeReplaced(ServletRequestAttributeEvent srae) {
    }
    }

HttpSession
  • Session 是服务器端会话技术。当浏览器访问 Web 服务器的资源时,服务器可以为每个用户浏览器创建一个 Session 对象,每个浏览器独占一个 Session 对象。

由于每个浏览器独占一个 Session,所以用户在访问服务器的资源时,可以把数据保存在各自的 Session 中。当用户再次访问该服务器中的其它资源时,其它资源可以从 Session 中取出数据,为用户服务。

  • HttpSessionListener

    • public interface HttpSessionListener extends EventListener {
      default void sessionCreated(HttpSessionEvent se) {
      }

    default void sessionDestroyed(HttpSessionEvent se) {
    }
    }

  • HttpSessionAttributeListener

    • public interface HttpSessionAttributeListener extends EventListener {
      default void attributeAdded(HttpSessionBindingEvent se) {
      }

    default void attributeRemoved(HttpSessionBindingEvent se) {
    }

    default void attributeReplaced(HttpSessionBindingEvent se) {
    }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号