• Java监听器listener的介绍


    Java监听器listener的介绍

    listener

    • 能做什么

      当web中某些动作发生之后,服务器就调用listener中对应的方法。

    • 内部机制

      接口回调

    Web监听器

    • 步骤

      1. 创建需要的监听器类,实现接口

      2. 注册|配置(有些不需要注册)监听器。

        servlet4.0可以用反射实现@WebListener()

        servlet2.5是需要在web.xml中配置

        <listener>
        	<listener-class>com.itheima.listener.MyRequestListener</listener-class>
        </listener>
        

    三个作用域的监听器(需要注册)

    request  ---httpServletRequest
    session  ---httpSession
    aapplication  --- ServletContext
    
    1. 用途

      主要是这3个域的创建与销毁时调用方法

      • ServletContextListener

      利用它来,在servletcontext创建的时候,

      1. 完成自己想要的初始化工作
      2. 执行自定义任务调度。 执行某一个任务。 Timer
      • HttpSessionListener

      统计在线人数

    2. ServletRequestListener

      • request创建:

      访问服务器上的任意资源都会有请求出现。

      访问 html: 会

      访问 jsp: 会

      访问 servlet: 会

      • request销毁:

      服务器已经对这次请求作出了响应。

    3. ServletContextListener

      • servletcontext创建:

      启动服务器的时候

      • servletContext销毁:

      关闭服务器. 从服务器移除项目

    4. HttpSessionListener

      • session的创建

      只要调用 getSession

      html: 不会

      jsp: 会

      servlet:会 要写getSession();

      • session的销毁

      超时 30分钟

      非正常关闭 销毁

      正常关闭服务器(序列化)

    监听三个作用域属性状态变更(需要注册)

    ​ servletContext --- ServletContextAttributeListener

    ​ request --- ServletRequestAttributeListener

    ​ session --- HttpSessionAttributeListener

    • 条件

      可以监听在作用域中值 添加 | 替换 | 移除的动作。

      调用的相应的方法。

    • 用法(重载,只是3方法的参数不同,查API就行了)

      @Override
      public void attributeAdded(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute 
           is added to a session.
        */
      }
      
      @Override
      public void attributeRemoved(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute
           is removed from a session.
        */
      }
      
      @Override
      public void attributeReplaced(HttpSessionBindingEvent sbe) {
        /* This method is invoked when an attibute
           is replaced in a session.
        */
      }
      

    监听httpSession里面存值的状态变更(不用注册)

    1. HttpSessionBindingListener

      监听对象与session 绑定session.setAttribute("键名",对象)和解除绑定session.removeAttribute("键名")的动作

    • 作用

      JavaBean被绑定了和解绑时调用

    • 步骤

      让JavaBean实现该接口

    1. HttpSessionActivationListener

      用于监听现在session的值 是 钝化 (序列化)还是活化 (反序列化)的动作

    • 作用

      钝化 (序列化)将内存中的数据存到硬盘中

      活化 (反序列化)将硬盘的数据加载到内存中

      session中的值可能会很多, 并且我们有很长一段时间不使用这个内存中的值, 那么可以考虑把session的值可以存储到硬盘上【钝化】,等下一次在使用的时候,在从硬盘上提取出来。 【活化】

    • 步骤

    • 创建

      让JavaBean实现该接口和Serializable接口

    • session中的值一段时间不用自动钝化的配置

    • 1.在tomcat里面 conf/context.xml 里面配置

      对所有的运行在这个服务器的项目生效

      2.在conf/Catalina/localhost/context.xml 配置

      对 localhost生效。 localhost:8080

      3.在自己的web工程项目中的 META-INF/context.xml

      只对当前的工程生效。

      存储地址 tomcat目录workCatalinalocalhostListenerDemoitheima

      maxIdleSwap : 1分钟不用就钝化
      directory : 钝化后的那个文件存放的目录位置。

      <Context>
      	<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
      		<Store className="org.apache.catalina.session.FileStore" directory="itheima"/>
      	</Manager>
      </Context>
      
  • 相关阅读:
    C#进阶-Linq-join
    C#进阶-Linq
    C#-string-stringBuilder
    C#-继承-多态
    Spring基础
    JQuery基本操作
    Oracle数据库基础操作
    AJAX前端后端
    AJAX异步提交(前端)
    js基本操作
  • 原文地址:https://www.cnblogs.com/richardwlee/p/10339250.html
Copyright © 2020-2023  润新知