http://sakyone.iteye.com/blog/471093
Listener的作用类似于load-on-startup的Servlet,在Web应用启动时被加载,在Web应用关闭时被销毁,Listener用来作为Web应用的后台服务,比load-on-startup的Servlet更早被加载到Servlet容器中。自定义的Listener类必须实现ServletContextListener接口,并实现该接口的两个方法:contextInitialized(ServletContextEvent)和contextDestroyed(ServletContextEvent),例如:
public class GetConnListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce){
try{
ServletContext application=sce.getServletContext();
String driver=application.getInitParameter("driver");
String url=application.getInitParameter("url");
String user=application.getInitParameter("user");
String password=application.getInitParameter("password");
Class.forName(driver);
Connection conn=DriverManager.getConnection(url, user, password);
application.setAttribute("conn", conn);
}catch(Exception e){
System.out.println("Listener中获取数据库连接出现异常:"+e.getMessage());
}
}
public void contextDestroyed(ServletContextEvent sce){
ServletContext application=sce.getServletContext();
Connection conn=(Connection)application.getAtrribute("conn");
if(conn!=null){
try{
conn.close();
conn=null;
}catch(Exception e){}
}
}
}
Listener配置:
<listener>
<listener-class>lee.GetConnListener</listener-class>
</listener>
相关推荐
关于所有ServletListener的介绍,非常有用的一个小东东
关于filter、servlet在web.xml配置及加载顺序
SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 1.创建一个类实现 ServletContextListener (具体实现哪个Listener根据情况来判断) 2.在类上加入注解 @WebListener 3.重写 context...
Servlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的八大Listener.docServlet中的...
详细的解释了servlet filter listener 额知识,本文档适合初学者阅读
servlet--listener 各种接口的简单例子 ,将每个接口的实现都简单的举例,特别容易学会,下载后直接 放到工作空间后运行
IBM Servlet Listener课件,的确非常好。学习的好资料。
@ServletComponentScan // 扫描filter servlet listener @MapperScan("cn.springBoot.dao") // mybatis 扫描,没有这个不能注入 @SpringBootApplication public class Application { // TRACE 日志文件级别 public ...
servlet_filter_listener
JAVA Servlet监听器listener学习资料,精心整理,望对各位大虾有帮助。
web.xml 中的listener、 filter、servlet 加载顺序及其详解
Servlet、Filter、Listener深入理解.docx
Java WEB 篇九 Java servlet、filter、listener、interceptor 之间的区别和联系?
里边用到了Servlet Filter Listener 很适合新手学习的小例子
本源码将详细介绍web.xml配置中servlet,filter,listener的加载顺序,可以让学习者更好的了解web.xml各种属性配置,自己写的东西,不足之处请大家见谅,顺便收点积分也好下资料,谢谢
servlet监听器案例,web.xml,加jsp,listener的配置和使用
Using Forms Listener Servlet with Oracle Applications 11i (Doc ID 201340.1)
script_data 为数据库脚本。 管理员默认为:123 密码 123... 界面采用Easyui, 后台为servlet控制(无使用框架) 方便初学者,可以学习jsp,easyui.... 实现基本的用户权限管理。 涉及:jsp tag,servlet listener 等的使用。