zoukankan      html  css  js  c++  java
  • Listener系列教材 (二)- 监听 Context

    对Context的监听分生命周期的监听,和Context上Attribute变化的监听两种。

    步骤1:编写 ContextListener
    步骤2:配置 web.xml
    步骤3:测试
    步骤4:编写 ContextAttributeListener
    步骤5:配置web.xml
    步骤6:testContext.jsp
    步骤7:测试

    步骤 1 : 编写 ContextListener

    ContextListener 实现接口ServletContextListener 
    有两个方法

    public void contextDestroyed(ServletContextEvent arg0) 


    对应当前web应用的销毁

    public void contextInitialized(ServletContextEvent arg0)


    对应当前web应用的初始化

    package listener;

    import javax.servlet.ServletContextEvent;

    import javax.servlet.ServletContextListener;

    public class ContextListener implements ServletContextListener {

        @Override

        public void contextDestroyed(ServletContextEvent arg0) {

            System.out.println("web 应用销毁  ");

        }

        @Override

        public void contextInitialized(ServletContextEvent arg0) {

            System.out.println("web 应用初始化 ");

        }

    }

    步骤 2 : 配置 web.xml

    配置监听器

    <listener>

        <listener-class>listener.ContextListener</listener-class>

    </listener>

    步骤 3 : 测试

    在启动tomcat以及当前web应用重载的时候可以观察到
    web应用的自动重载需要如下前提:
    1. 配置: server.xml中对应的<context配置 的属性 @reloadable="true"
    2. 某个servlet 发生了变化,导致这个web应用自动重启

    测试

    步骤 4 : 编写 ContextAttributeListener

    ContextAttributeListener 实现接口ServletContextAttributeListener,
    分别提供如下方法:
     

    public void attributeAdded(ServletContextAttributeEvent e)


    监听属性的增加

    public void attributeRemoved(ServletContextAttributeEvent e)


    监听属性的移除

    public void attributeReplaced(ServletContextAttributeEvent e)


    监听属性的替换

    package listener;

    import javax.servlet.ServletContextAttributeEvent;

    import javax.servlet.ServletContextAttributeListener;

    public class ContextAttributeListener implements ServletContextAttributeListener {

        @Override

        public void attributeAdded(ServletContextAttributeEvent e) {

            System.out.println("增加属性 ");

            System.out.println("属性是" + e.getName());

            System.out.println("值是" + e.getValue());

        }

        @Override

        public void attributeRemoved(ServletContextAttributeEvent e) {

            // TODO Auto-generated method stub

            System.out.println("移除属性 ");

        }

        @Override

        public void attributeReplaced(ServletContextAttributeEvent e) {

            // TODO Auto-generated method stub

            System.out.println("替换属性");

        }

    }

    步骤 5 : 配置web.xml

    配置ContextAttributeListener

    <listener>

        <listener-class>listener.ContextAttributeListener</listener-class>

    </listener>

    步骤 6 : testContext.jsp

    故意造成Context属性的增加,替换和移除

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <%

        application.setAttribute("test", 1);

        application.setAttribute("test", 2);

        application.removeAttribute("test");

    %>

    步骤 7 : 测试

    访问页面进行测试

    http://127.0.0.1/testContext.jsp

    测试


    更多内容,点击了解: https://how2j.cn/k/listener/listener-context/605.html

  • 相关阅读:
    通过SQLServer的数据库邮件来发送邮件
    sql生成数据库的序列号
    存储过程备份数据库
    LED客显的类
    坦克大战java版
    连连看java版
    贪吃蛇java版
    分享插件 javascript
    js实现上传图片及时预览
    json返回date类型转为字符串
  • 原文地址:https://www.cnblogs.com/Lanht/p/12789437.html
Copyright © 2011-2022 走看看