javax.naming.NameNotFoundException | JNDI查找

时间:2016-04-26 06:20:39

标签: java xml tomcat servlets

我在这个网站上发现了很多与问题相关的问题和答案,但找不到我的问题的解决方案,所以最后不得不再次发布同样的问题!

我正在创建一个简单的servlet DBCPDataSourceExample ,并从我在{{1}中提供的 DataSource 属性获取连接}& Apache Tomcat v8 应用服务器的server.xml

以下是 context.xml servlet:

DBCPDataSourceExample

import javax.sql.DataSource; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DBCPDataSourceExample extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse respone) throws IOException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; Context ctx ; try { try { ctx = new InitialContext(); Context ic= (Context) ctx.lookup("java:/comp/env"); DataSource dts = (DataSource) ic.lookup("jdbc/harsh"); connection = dts.getConnection(); } catch (NamingException e) { e.printStackTrace(); } statement = connection.prepareStatement("select * from stories"); resultSet = statement.executeQuery(); while (resultSet.next()) { System.out.println("storyId: " + resultSet.getString("storyId")); System.out.println("storyTitle: " + resultSet.getString("storyTitle")); } } catch (SQLException e) { e.printStackTrace(); } finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException e) {e.printStackTrace();} if (statement != null) try { statement.close(); } catch (SQLException e) {e.printStackTrace();} if (connection != null) try { connection.close(); } catch (SQLException e) {e.printStackTrace();} } } } server.xml映射:

Resource

<GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> <Resource name="jdbc/MyDB" global="jdbc/MyDB" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/storyBoard" username="root" password="" maxActive="100" maxIdle="20" minIdle="5" maxWait="10000"/> </GlobalNamingResources> context.xml映射:

Resource

如果 <Resource name="jdbc/harsh" global="jdbc/MyDB" auth="container" type="javax.sql.DataSource" /> 代码出现问题我无法发现。请帮忙!

以下是来自控制台的堆栈跟踪:

xml

stacktrace指向的代码行是:

javax.naming.NameNotFoundException: Name [jdbc/harsh] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at com.dbcp.DBCPDataSourceExample.doGet(DBCPDataSourceExample.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

我们在server.xml中添加资源,同时我们需要创建一个资源链接,其中context.xml是必需的。

在我们的例子中,我们将context.xml保存在war的META-INF中,样本条目看起来像

<ResourceLink name="email.host.name" global="email.host.name" type="java.lang.String"/>

请验证您的context.xml条目。

还要确保已将mysql驱动程序放在tomcat的lib目录中。

答案 1 :(得分:0)

在您的Server.xml文件中,我认为您需要在url中提供端口号。 例如url =&#34; jdbc:mysql:// localhost:3306 / storyBoard&#34;