gpt4 book ai didi

javax.naming.NamingException : Name is not bound to a Context

转载 作者:行者123 更新时间:2023-12-02 01:15:06 26 4
gpt4 key购买 nike

我的监听器代码如下

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

}

}

我的上下文文件位于项目根目录中,如下所示

<Context path="/newsfeed" docBase="D:\My Projects\springmvc\newsfeed\src\main\webapp">
<Resource name="datasource"
type="javax.sql.DataSource"
auth="Container"
maxActive="10"
maxIdle="3"
maxWait="10000"
username="gaurav"
password="gaurav"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/publisher?autoReconnect=true" />
</Context>

Web.xml如下

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.javaimplant.newsfeed.Init.Init</listener-class>
</listener>
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>org.javaimplant.newsfeed.Servlets.HomeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>data</servlet-name>
<servlet-class>org.javaimplant.newsfeed.Servlets.DataSourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>data</servlet-name>
<url-pattern>/data</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<resource-ref>
<description>dataSource</description>
<res-ref-name>datasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

运行代码时出现以下错误。我不知道为什么我的应用程序无法从 xml 文件读取上下文。请帮忙

javax.naming.NamingException:名称未绑定(bind)到上下文 在 org.javaimplant.newsfeed.Init.Init.contextInitialized(Init.java:29)

最佳答案

好的,Eclipse 在工作区中维护自己独立的服务器配置,您必须在那里进行更改

关于javax.naming.NamingException : Name is not bound to a Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57663742/

26 4 0