本文共 3343 字,大约阅读时间需要 11 分钟。
当Web应用在Web容器中运行时,Web应用内部会不断地发生各种事件,如Web应用被启动,Web应用被停止,用户的Session开始与结束,用户请求到达等,通常来说,这些Web事件对用户是透明的。实际上,ServletAPI提供了大量监听器来监听Web应用的内部事件,从而允许当Web内部事件发生的时候回调事件监听器内的方法。
使用监听器有两个步骤:1). 定义Listener实现类 2). 通过Annotation或在web.xml文件中配置该Listener
1. 实现Listener类
与AWT编程完全相似,监听不同Web事件的监听器也不相同。常用的Web事件监听器接口有如下几个:
ServletContextListener:用于监听Web应用的启动和关闭
ServletContextAttributeListener:用于监听ServletContext范围(Application)内属性的改变
ServletRequestListener:用于监听用户请求
ServletRequestAttributeListener:用于监听ServletRequest范围(Request)内属性的改变
HttpSessionListener:用于监听用户Session的开始和结束
HttpSessionAttributeListener:用于监听HttpSession范围(Session)内属性的改变
下面以Servlet为例介绍Listener的开发和使用。
ServletContextListener接口定义了如下两个方法:
cotextInitialized(ServletContextEvent sce):启动Web应用时,系统会调用Listener的该方法
contextDestroyed(ServletContextEvent sce):关闭Web应用时,系统会调用Listener的该方法
下面将创建一个获取数据库连接的Listener,该Listener会在应用启动时获取数据库连接,并将取到的数据库连接设置成Application范围内的属性,下面是该Listener的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | @WebListener //使用Annotation的方式配置此Listener public class GetConnectionListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { //应用启动时,将调用这个方法 try { ServletContext application = sce.getServletContext(); //获取的是Web应用的配置参数,而不是像Servlet和Filter获取本身的配置参数。 //这是因为配置Listener十分简单,只需要简单地指定Listener实现类即可,不能配置初始化参数。 String driverClassName = appplication.getInitParameter( "driverClassName" ); String url = application.getInitParameter( "connURL" ); String username = application.getInitParameter( "username" ); String password = application.getInitParameter( "password" ); Class.forName(driver); Connection connection = DriverManager.getConnection(url, username, password); application.setAttribute( "conection" , connection); } catch (Exception e) { System.out.println( "获取数据库连接异常:" + e.getMessage()); } } public void contextDestroyed(ServletContextEvent sce) { //应用关闭时,将调用这个方法 ServletContext application = sce.getServletContext(); Connection connection = (Connection)application.getAttribute( "connection" ); if (connection != null && connection.isOpen()) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } |
2. 配置Listener
配置Listener只需要向Web应用注册Listener实现类即可,为Web应用配置Listener也有两种方式: 1). 使用@WebListener修饰Listener的实现类即可 2). 在web.xml文档中使用<listener>标签进行注册
使用@WebListener时通常无需指定任何属性,在web.xml中注册Listener也只需要配置<listener-class>元素即可,以下是一个在web.xml文件中配置此Listener的片段:
1 2 3 | < listener > < listener-class >com.abc.GetConnectionListener</ listener-class > </ listener > |
3. 其他几个Listener的简单介绍
ServletContextAttributeListener:用于监听ServletContext(Application)范围内属性的变化,实现该接口的监听器需要实现以下方法:
attributeAdded(ServletContextAttributeEvent event):当程序把一个属性存入Application范围时触发该方法
attributeRemoved(ServletContextAttributeEvent event):当程序把一个属性从Application范围删除时触发该方法
attributeReplaced(ServletContextAttributeEvent event):当程序替换一个Application范围属性时触发该方法
ServletRequestListener:用于监听用户请求,实现该接口的监听器需要实现以下方法:
requestInitialized(ServletRequestEvent event):用户请求到底、被初始化时触发该方法
requestDestroyed(ServletRequestEvent event):用户请求结束、被销毁时触发该方法
ServletRequestAttributeListener,用于监听ServletRequest范围(Request)内属性的改变,接口中定义的方法和ServletContextAttributeListener类此,此处不赘述。
转载地址:http://xwdel.baihongyu.com/