eclipse进行演示:
1.创建 Dynamic Web Project ,Dynamic Web module version选择3.0
![](https://img-blog.csdnimg.cn/img_convert/337fc99851f0f81bad9fd442482b0297.png)
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服务。打印结果如下
![](https://img-blog.csdnimg.cn/img_convert/4633cdd68b01d40cc9b4ce9cb7c8c37a.png)
注意事项,每次修改配置或者java代码后,要重新编译,否则不起作用
eclipse进行演示:
![](https://img-blog.csdnimg.cn/img_convert/c688d7bfdee321b3841ca013157472f3.png)
![](https://img-blog.csdnimg.cn/img_convert/df9ad9660540b12a49b375146c35cca9.png)
代码如下
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://img-blog.csdnimg.cn/img_convert/c5ae57d913fb7b6450ba2ec8af614d36.png)
停止启动
![](https://img-blog.csdnimg.cn/img_convert/42e1ed5d83d7c441d9ebc78bf5d76a6a.png)