Tomcat 6 JNDI配置及测试

(1)配置:

D:/apache-tomcat-6.0.30/conf/Catalina/localhost 文件夹下添加:TestWorkFlow.xml,其中TestWorkFlow为项目的context-root 

<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <Context path="/HaierTestWorkFlow" reloadable="true" docBase="D:/Java/HaierTestWorkFlow/WebRoot"> <Loader delegate="true"/> <Resource name="jdbc/oracle73" auth="Container" type="javax.sql.DataSource" username="auth" password="ouccs" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@222.195.151.73:1521:orcl" maxActive="8" maxIdle="4"/> </Context>  

(2)新建Jndi类

2.1 Jndi.java

package org.ouc.webNewTech.JNDI; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import oracle.jdbc.pool.OracleDataSource; public class Jndi { /** * @param args */ public static void testConnection() { // TODO Auto-generated method stub String message = "Not Connected"; Connection conn=null; ResultSet rst = null; Statement stmt = null; Context ctx; try { ctx = new InitialContext(); Context envContext = (Context) ctx.lookup("java:comp/env"); Object datasourceRef=envContext.lookup("jdbc/oracle73"); DataSource ds=(DataSource)datasourceRef; conn=ds.getConnection(); if (conn != null) { message = "Got Connection " + conn.toString() + ", "; stmt = conn.createStatement(); rst = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL"); System.out.println("Jndi Test Succeed!"); } } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }  

(3)测试:index.jsp

http://wiki.apache.org/tomcat/FAQ/Miscellaneous#Q22

 

Can I access Tomcat's JNDI provider from outside tomcat?

 

        Not at this time.

所以,必须在tomcat容器中才能使用JNDI

<body> This is my JSP page. <br> <% Jndi.testConnection(); %> </body> 

如果通过main函数获取jndi

         public static void main(String[] args) { testConnection(); } 

会出现如下错误:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) at javax.naming.InitialContext.lookup(InitialContext.java:392) at org.ouc.webNewTech.JNDI.Jndi.testConnection(Jndi.java:29) at org.ouc.webNewTech.JNDI.Jndi.main(Jndi.java:57)  

 

 

你可能感兴趣的:(apache,tomcat,File,express,applet,permissions)