我的监听器代码如下
package org.javaimplant.newsfeed.Init;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.javaimplant.newsfeed.Servlets.DataSourceServlet;
import org.javaimplant.newsfeed.data.DataAccessObject;
public class Init implements ServletContextListener {
private Logger logger=Logger.getLogger(this.getClass());
public void contextInitialized(ServletContextEvent sce)
{
ServletContext servletContext = sce.getServletContext();
try {
contextInitialized2(servletContext);
}
catch (Exception e)
{
logger.error("Initialization failed.", e);
throw new RuntimeException(e);
}
logger.debug("Initialization succeeded.");
}
private void contextInitialized2(ServletContext servletContext)
throws Exception
{
InitialContext enc = new InitialContext();
NamingEnumeration<NameClassPair> list = enc.list("java:comp/env/datasource");
while (list.hasMore())
{
System.out.println(list.next().getName());
}
Context compContext = (Context) enc.lookup("java:comp/env");
DataSource dataSource = (DataSource) compContext.lookup("datasource");
// DataAccessObject.setDatasource(dataSource);
DataSourceServlet.setDataSource(dataSource);
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}
运行代码时出现以下错误。我不知道为什么我的应用程序无法从 xml 文件中读取上下文。请帮忙
javax.naming.NamingException:名称未绑定到 org.javaimplant.newsfeed.Init.Init.contextInitialized(Init.java:29) 处的上下文
收到一只叮咚
相关分类