@WebListener 註解方式實現監聽

來源:互聯網
上載者:User

標籤:index   pre   ada   app   選擇   highlight   XML   splay   html   

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;@WebListenerpublic 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代碼後,要重新編譯,否則不起作用

 

  

@WebListener 註解方式實現監聽

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.