@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.
      */
    }
}

进行启动

停止启动

原文地址:https://www.cnblogs.com/weibanggang/p/9452864.html