ServletContextListener的用法和实例

ServletContextListener:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
ServletContextListener能监听到你的应用程序的启动和销毁。



例 2.2.1.1

package com;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements
ServletContextListener {
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("Destroyed 被执行");
    }
    public void contextInitialized(ServletContextEvent arg0) {
        ServletContext sc=arg0.getServletContext();
        System.out.println("马克-to-win Initialized 被执行"+sc.getMajorVersion());
    }
}



web.xml中加入一段话:

  <listener>
    <listener-class>com.MyServletContextListener</listener-class>
  </listener>


随便运行一个servlet或jsp,启动tomcat的过程中,你可以发现在console中其中有这么一段话:

马克-to-win Initialized 被执行2

随便改动一个java文件,一保存,应用程序被重新加载,过去的应用被卸载,于是contextDestroyed被执行。console当中也会出现下面的话:

Destroyed 被执行





例 2.2.1.2

下面是Listener的一个实际应用,和单态数据源应用相比,Listener数据源是启动你的web应用时启动的,不是第一个用户访问时启动的,对于用户来讲,体验会好一点。但有没有人访问,都会启动数据源。


package com;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
public class MyServletContextListener implements ServletContextListener {
    public void contextDestroyed(ServletContextEvent arg0) {
        ServletContext context = arg0.getServletContext();
        MysqlConnectionPoolDataSource ds=(MysqlConnectionPoolDataSource)context.getAttribute("mysqlds");
/*参见java第三章的内存泄露*/       
        ds=null;
        context.removeAttribute("mysqlds");
        System.out.println("Destroyed 被执行");
    }
    public void contextInitialized(ServletContextEvent arg0) {
        ServletContext context = arg0.getServletContext();
        DBConForListener con = new DBConForListener();
        MysqlConnectionPoolDataSource ds = con.getDs();
        context.setAttribute("mysqlds", ds);
        System.out.println("contextInitialized3");
    }
}






package com;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
public class DBConForListener {
    private MysqlConnectionPoolDataSource ds;
    public DBConForListener() {
        ds = new MysqlConnectionPoolDataSource();
        ds.setURL("jdbc:mysql://localhost:3306/test");
        ds.setUser("root");
        ds.setPassword("1234");
    }
    public MysqlConnectionPoolDataSource getDs() {
        return ds;
    }
}





package com;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

public class ServletHello1 extends HttpServlet {
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        ServletContext sc = this.getServletContext();
        MysqlConnectionPoolDataSource ds = (MysqlConnectionPoolDataSource) sc
                .getAttribute("mysqlds");
        Connection con;
        try {
            con = ds.getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from login");
            while (rs.next()) {
                System.out.println(rs.getString("id"));
                System.out.println(rs.getString("name"));
            }
            rs.close();
            stmt.close();
            con.close();
            System.out.println("ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


运行servlet,console当中输出的结果是:

1
q
2
qq
2.5
qqq
3
qqq
ok