zoukankan      html  css  js  c++  java
  • @WebListener 注解方式实现监听

    1.创建 Dynamic Web Project ,Dynamic Web module version选择3.0

     

    2.在自动生成 的web.xml配置,增加 metadata-complete="false"

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <javaee:web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     3 xmlns="http://Java.sun.com/xml/ns/javaee"
     4 xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
     5 xmlns:web="http://java.sun.com/xml/ns/javaee" 
     6 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     7  metadata-complete="false" version="3.0">
     8   <javaee:display-name></javaee:display-name>
     9   <javaee:welcome-file-list>
    10     <javaee:welcome-file>index.html</javaee:welcome-file>
    11   </javaee:welcome-file-list>
    12 </javaee:web-app>

    3.创建监听类,在监听类头部增加 注解 @WebListener

    package com.xhkj.listener;
    import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class MyServletContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("===========================MyServletContextListener销毁"); } @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("===========================MyServletContextListener初始化"); System.out.println(sce.getServletContext().getServerInfo()); } }

    4.启动tomcat服务。打印结果如下

    5 注意事项,每次修改配置或者java代码后,要重新编译,否则不起作用

    6 代码所在地址

    https://files.cnblogs.com/files/xhkj/TestWebListener.rar

      

  • 相关阅读:
    ansible 通过堡垒机/跳板机 访问目标机器需求实战(ssh agent forward)
    运维标准化与流程化建设
    运维与自动化运维发展方向
    文件atime未变问题的研究
    ansible 任务委派 delegate_to
    ansible 调优
    windows中安装模拟器后修改模拟器中的hosts方法
    负载均衡服务器主要考量三个指标
    DRBD+NFS+Keepalived高可用环境
    sshpass
  • 原文地址:https://www.cnblogs.com/xhkj/p/7440589.html
Copyright © 2011-2022 走看看