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

标签 java maven servlets listener jndi

我的监听器代码如下

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/

相关文章:

java - Java 中的 "caller"和 Ruby 中的 "receiver"一样吗?

java - 如何将测试类包含到 Maven jar 中并执行它们?

java - 为什么 jHipster 使用 oracle 12c 配置?使用oracle 11g配置有什么问题吗?

java - 如何在 Spock 中模拟 HttpServletRequest

java - 我应该关闭 servlet 输出流吗?

Java vector : How to find out the would-be index of an item

java - 如何在 Eclipse 中为新的 JUnit 测试默认源文件夹?

javascript - 如果 JavaScript 中的条件总是失败

客户端关闭连接后Java TCPSocket中断

java - 测量驻留在单独项目中的 Selenium 测试的代码覆盖率