怎样使用ServletContextListener接口
1个回答
推荐于2016-09-03 · 知道合伙人软件行家
关注
展开全部
package com.database;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributesListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.database.DbConnection;
public class DatabaseContextListener implements ServletContextListener {
private ServletContext context = null;
private Connection conn = null;
public DatabaseContextListener() {
}
//该方法在ServletContext启动之后被调用,并准备好处理客户端请求
public void contextInitialized(ServletContextEvent event) {
this.context = event.getServletContext();
conn = DbConnection.getConnection;
// 这里DbConnection是一个定制好的类用以创建一个数据库连接
context = setAttribute(”dbConn”,conn);
}
//这个方法在ServletContext 将要关闭的时候调用
public void contextDestroyed(ServletContextEvent event){
this.context = null;
this.conn = null;
}
}
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributesListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.database.DbConnection;
public class DatabaseContextListener implements ServletContextListener {
private ServletContext context = null;
private Connection conn = null;
public DatabaseContextListener() {
}
//该方法在ServletContext启动之后被调用,并准备好处理客户端请求
public void contextInitialized(ServletContextEvent event) {
this.context = event.getServletContext();
conn = DbConnection.getConnection;
// 这里DbConnection是一个定制好的类用以创建一个数据库连接
context = setAttribute(”dbConn”,conn);
}
//这个方法在ServletContext 将要关闭的时候调用
public void contextDestroyed(ServletContextEvent event){
this.context = null;
this.conn = null;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询