这里的INITIAL_CONTEXT_FACTORY跟url该填什么?

解决方案 »

  1.   

    在TOMCAT中访问JNDI需要在web.xml中进行一下引用,不能仅仅在虚拟目录描述文件中声明。
      

  2.   

    TO:xiangbo520(充栋汗牛) 
    web.xml中该如何引用?望指教!
      

  3.   

    JNDI 的配置如下:在你的 源文件下面根目录或者 /META-INF/下面放置:
    jndi.properties 文件。内容就是你在 
    properties.put()  方法里面 put 进去的那些参数设置。编译之后他就到了 WEB-INF/classes 或者 /META-INF/ 下面去了。还有一种方法就是 在 资源管理中用一个 jndi 表示一个字符串,他代表你的 jndi.properties 文件在什么地方,这样的话你的 jndi.properties 文件就可以不用放到 应用程序所在的目录下面去了而是可以放到任何地方,或者你用 URL 表示然后放到一个 FTP 服务器上都行, 只要你的用 properties.load(url.openStream()); 读那个文件就行了。
      

  4.   

    虚拟目录描述:
    <Context path="/DBTest" docBase="DBTest"
            debug="5" reloadable="true" crossContext="true">
      <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
                   maxActive="100" maxIdle="30" maxWait="10000"
                   username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/></Context>web.xml文件中加入
    <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/TestDB</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>
    参考上面的进行修改
    使用
    Context initContext = new InitialContext();
    Context envContext  = (Context)initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
    Connection conn = ds.getConnection();
    具体请参考http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
    这是TOMCAT的自带官方文档
      

  5.   

    同意楼上的意见,再这里就是需要修改以下TOMCAT中访问JNDI需要在web.xml中进行一下引用,不能仅仅在虚拟目录描述文件中声明。