2019独角兽企业重金招聘Python工程师标准>>>
在 http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/给了简洁的介绍:
The listener is something sitting there and wait for specified event happened, then “hijack” the event and run its own event.文中给出了这样的例子:
package com.mkyong.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class MyAppServletContextListener implements ServletContextListener{@Overridepublic void contextDestroyed(ServletContextEvent arg0) {System.out.println("ServletContextListener destroyed");}//Run this before web application is started@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("ServletContextListener started"); }
}
<web-app ...><listener><listener-class>com.mkyong.listener.MyAppServletContextListener </listener-class></listener>
</web-app>
//...
Dec 2, 2009 10:11:46 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.20ServletContextListener started <-------------- Your code here, before we application --->Dec 2, 2009 10:11:46 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
//...
INFO: Server startup in 273 ms
1.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文. 3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器创建<listener></listener>中的类实例,即创建监听.
5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得
ServletContext = ServletContextEvent.getServletContext();
context-param的值 = ServletContext.getInitParameter("context-param的键");
6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.
换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.
7.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.
8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.