ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
MyListener.java ~~~ import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener() public class MyListener implements ServletContextListener{ public MyListener() { } public void contextInitialized(ServletContextEvent sce) { System.out.println("初始化方法执行"); } public void contextDestroyed(ServletContextEvent sce) { System.out.println("销毁方法执行"); } } ~~~ 或者 FirstServletContextListener .java ~~~ package zyw; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class FirstServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("servletcontext初始化了"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("servletcontext销毁了"); } } ~~~ web.xml ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <listener> <listener-class>zyw.FirstServletContextListener</listener-class> </listener> </web-app> ~~~ ![](https://box.kancloud.cn/89986d92a4191ab8e3bea8682c7bed53_326x118.png) ![](https://box.kancloud.cn/4a5ec26616dc4354bd22cc48b98a4cd5_221x96.png)