zoukankan      html  css  js  c++  java
  • @WebListener 注解方式实现监听(eclipse和idea)

    eclipse进行演示:

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

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

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

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

    package com;
    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服务。打印结果如下

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

    eclipse进行演示:

    代码如下

    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 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.
          */
            System.out.println("开始初始化");
        }
    
        public void contextDestroyed(ServletContextEvent sce) {
          /* This method is invoked when the Servlet Context 
             (the Web application) is undeployed or 
             Application Server shuts down.
          */
            System.out.println("开始销毁");
        }
    
        // -------------------------------------------------------
        // 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.
          */
        }
    
        public void attributeRemoved(HttpSessionBindingEvent sbe) {
          /* This method is called when an attribute
             is removed from a session.
          */
        }
    
        public void attributeReplaced(HttpSessionBindingEvent sbe) {
          /* This method is invoked when an attibute
             is replaced in a session.
          */
        }
    }

    进行启动

    停止启动

  • 相关阅读:
    字符串属于对象而非基本数据类型,为什么?
    比较字符串内容用equals,比较字符串的地址用==,为什么?
    服务器数据恢复方法,磁盘阵列崩溃怎么恢复数据
    服务器磁盘阵列数据恢复,raid5两块硬盘掉线数据恢复方法
    服务器存储共享文件夹丢失数据恢复检测报告
    SUN平台服务器光纤共享存储互斥失败如何恢复数据?
    服务器磁盘阵列存储瘫痪数据恢复成功案例
    EMC CX4-480服务器raid磁盘数据恢复案例
    服务器数据恢复方法_存储raid硬盘离线数据恢复案例
    山西某公司NetApp存储不小心删除文件数据恢复成功案例
  • 原文地址:https://www.cnblogs.com/weibanggang/p/9452864.html
Copyright © 2011-2022 走看看