摘要:
当web应用在web容器中运行时,web应用内部会不断地发生各种事件:如web应用被启动东,web应用被停止,用户session开始,用户session结束,用户请求到达等等……,这些web事情对开发者是透明的,通过监听器我们可以知道web容器里面发生的任何事情。
ServletContextListener
常用的监听器有
ServletContextListener:用来监听web应用的启动和关闭
contextInitialized(ServletContextEvent arg0) 当web应用被启动的时候执行
contextDestroyed(ServletContextEvent arg0)当web应用被启动关闭时执行
监听的配置有两种方式:
第一种是web注解的方式
第二种是在xml文件里面配置
1 package com.huayu.listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 6 public class Listener1 implements ServletContextListener{ 7 8 @Override 9 public void contextDestroyed(ServletContextEvent arg0) { 10 // TODO Auto-generated method stub 11 System.out.println("=========应用关闭============"); 12 } 13 14 @Override 15 public void contextInitialized(ServletContextEvent arg0) { 16 // TODO Auto-generated method stub 17 System.out.println("===应用启动========="); 18 } 19 20 }
web.xml
1 <listener> 2 <listener-class>com.huayu.listener.Listener1</listener-class> 3 </listener>
下面演示:
写一个ServletContextListener监听器,当web应用启动是链接orcl数据库,在jsp页面中将emp表里面的数据以表格的形式打印出来,当web应用关闭时断开与orcl数据库的链接
1 package com.huayu.listener; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.SQLException; 7 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletContextEvent; 10 import javax.servlet.ServletContextListener; 11 12 public class Listener1 implements ServletContextListener{ 13 14 @Override 15 public void contextDestroyed(ServletContextEvent arg0) { 16 17 ServletContext application=arg0.getServletContext(); 18 Connection conn=(Connection) application.getAttribute("connoracle"); 19 if(conn!=null){ 20 21 try { 22 conn.close(); 23 } catch (SQLException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 } 28 29 } 30 31 @Override 32 public void contextInitialized(ServletContextEvent arg0) { 33 // TODO Auto-generated method stub 34 String driver="oracle.jdbc.driver.OracleDriver"; 35 String url="jdbc:oracle:thin:@localhost:1521:orcl"; 36 String user="scott"; 37 String pass="ztoracle"; 38 39 ServletContext application=arg0.getServletContext(); 40 41 try { 42 Class.forName(driver); 43 Connection conn=DriverManager.getConnection(url, user, pass); 44 application.setAttribute("connoracle", conn); 45 46 } catch (Exception e) { 47 throw new RuntimeException(); 48 49 50 } 51 52 } 53 54 }
1 <%@page import="java.sql.ResultSetMetaData"%> 2 <%@page import="java.sql.ResultSet"%> 3 <%@page import="java.sql.PreparedStatement"%> 4 <%@page import="java.sql.Connection"%> 5 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%> 6 <% 7 String path = request.getContextPath(); 8 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 9 %> 10 11 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 12 <html> 13 <head> 14 <base href="<%=basePath%>"> 15 16 <title>My JSP ‘index.jsp‘ starting page</title> 17 <meta http-equiv="pragma" content="no-cache"> 18 <meta http-equiv="cache-control" content="no-cache"> 19 <meta http-equiv="expires" content="0"> 20 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 21 <meta http-equiv="description" content="This is my page"> 22 <!-- 23 <link rel="stylesheet" type="text/css" href="styles.css"> 24 --> 25 </head> 26 27 <body> 28 <% 29 Connection conn=(Connection)application.getAttribute("connoracle"); 30 PreparedStatement ps=conn.prepareStatement("select * from emp"); 31 ResultSet rs=ps.executeQuery(); 32 ResultSetMetaData rsm=rs.getMetaData(); 33 int count=rsm.getColumnCount(); 34 %> 35 <table border="1"> 36 <% 37 while(rs.next()){ 38 %> 39 <tr> 40 <% 41 for(int i=0;i<count;i++){ 42 43 %> 44 <td> 45 <% 46 rs.getString(i+1); 47 %> 48 49 50 </td> 51 <% } %> 52 </tr> 53 <%} %> 54 </table> 55 56 </body> 57 </html>
时间: 2024-12-18 00:14:09