名称[jdbc / mydb]未绑定在此Context中

时间:2014-03-20 21:10:29

标签: java-ee tomcat7 jndi derby

我看到这个问题已经提出好几次,我经历了所有这些问题。但我仍然无法解决我的问题。

有人能帮助我查明我做错了吗?

当我尝试访问database.jsp时收到以下错误消息:

    HTTP Status 500 - An exception occurred processing JSP page /database.jsp at line 24

type Exception report

message An exception occurred processing JSP page /database.jsp at line 24

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /database.jsp at line 24

21:         <%
22:                 Context init = new InitialContext();
23:                 Context env = (Context) init.lookup("java:/comp/env/");
24:                 DataSource data = (DataSource) env.lookup("jdbc/mydb");
25:                 Connection conn = data.getConnection();
26:                 Statement stmt = conn.createStatement();
27: 


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

javax.servlet.ServletException: javax.naming.NameNotFoundException: Name [jdbc/mydb] is not bound in this Context. Unable to find [jdbc].
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
    org.apache.jsp.database_jsp._jspService(database_jsp.java:185)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

以下是有关我的安装的一些信息: 我的电脑在Mac OS X上运行.Web服务器是Tomcat版本7.0.52,安装在/Users/username/Dropbox/Server/apache-tomcat-7.0.52下。我试图将Derby 10.10.1.1作为嵌入式数据库运行。我在文件夹/Users/username/Dropbox/Server/apache-tomcat-7.0.52/lib下复制了derby.jar。我在PATH文件中写了以下内容(我不确定它是否相关):

export DERBY_HOME=/Users/username/Dropbox/Server/apache-tomcat-7.0.52/lib
export PATH=$PATH:$DERBY_HOME/bin

以下是web.xml文件的内容:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
    <resource-ref>
       <description>DB Connection</description>
       <res-ref-name>jdbc/mydb</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
    </resource-ref>
  </description>

</web-app>

context.xml中

<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be loaded for each web application -->
<Context>
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

server.xml中

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
        <!-- Added by LC #2 @19/3/2014 -->
            <!-- Global Datasource for Derby mydb database -->
            <Resource name="jdbc/mydb"
                     type="javax.sql.DataSource"  auth="Container"
                     description="Derby database for Stocktails"
                     maxActive="20" maxIdle="10" maxWait="-1"
                     username="" password="" 
                     driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
                     url="jdbc:derby:/Users/username/Dropbox/Server/apache-tomcat-7.0.52/databases/mydb"/>
        <!-- End #2 -->

  </GlobalNamingResources>

  <Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

        <!-- Added by LC #1 @19/3/2014 -->
        <Context
           docBase="ROOT" path="" debug="0"
           reloadable="true">
        </Context>
        <!-- End #1 -->

      </Host>
    </Engine>
  </Service>
</Server>

最后,这是database.jsp页面:

<html>
<head>
  <title>Quote Query</title>
</head>
<body>
  <h1>retrieve stock quotes</h1>
  <h3>Choose Ticker(s):</h3>
  <form method="get">
    <input type="checkbox" name="ticker" value="AAPL">AAPL
    <input type="checkbox" name="ticker" value="MSFT">MSFT
    <input type="submit" value="Query">
  </form>

  <%
    String[] tickers = request.getParameterValues("ticker");
    if (tickers != null) {
  %>


        <%@ page import="javax.naming.*,java.sql.*,javax.sql.*" %>
        <%
                Context init = new InitialContext();
                Context env = (Context) init.lookup("java:/comp/env/");
                DataSource data = (DataSource) env.lookup("jdbc/mydb");
                Connection conn = data.getConnection();
                Statement stmt = conn.createStatement();

                String sqlStr = "SELECT * FROM Quotes WHERE Ticker IN (";
                sqlStr += "'" + tickers[0] + "'";  // First ticker
                for (int i = 1; i < tickers.length; ++i) {
                   sqlStr += ", '" + tickers[i] + "'";  // Subsequent tickers need a leading commas
                }
                sqlStr += ") ORDER BY Date ASC";

                // for debugging
                System.out.println("Query statement is " + sqlStr);

                ResultSet rset = stmt.executeQuery(sqlStr);
        %>

      <hr>
      <form method="get" action="quotes.jsp">
        <table border=1 cellpadding=5>
          <tr>
            <th>Ticker</th>
            <th>Date</th>
            <th>PriceOpen</th>
            <th>PriceHigh</th>
            <th>PriceLow</th>
            <th>PriceClose</th>
            <th>Volume</th>
            <th>PriceAdjClose</th>
          </tr>
  <%
      while (rset.next()) {
        int id = rset.getInt("id");
  %>
          <tr>
            <td><input type="checkbox" name="id" value="<%= id %>"></td>
            <td><%= rset.getString("Ticker") %></td>
            <td><%= rset.getString("Date") %></td>
            <td>$<%= rset.getInt("PriceOpen") %></td>
            <td><%= rset.getInt("PriceHigh") %></td>
            <td><%= rset.getString("PriceLow") %></td>
            <td><%= rset.getString("PriceClose") %></td>
            <td>$<%= rset.getInt("Volume") %></td>
            <td><%= rset.getInt("PriceAdjClose") %></td>
          </tr>
  <%
      }
  %>
        </table>
        <br>
        <input type="submit" value="Order">
        <input type="reset" value="Clear">
      </form>
      <a href="<%= request.getRequestURI() %>"><h3>Back</h3></a>
  <%
      rset.close();
      stmt.close();
      conn.close();
    }
  %>
</body>
</html>

日志

Mar 21, 2014 7:57:26 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/lcazarre/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Mar 21, 2014 7:57:26 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did not find a matching property.
Mar 21, 2014 7:57:28 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 21, 2014 7:57:28 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 21, 2014 7:57:28 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 3227 ms
Mar 21, 2014 7:57:28 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 21, 2014 7:57:28 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.52
Mar 21, 2014 7:57:28 AM org.apache.catalina.realm.UserDatabaseRealm startInternal
SEVERE: Exception looking up UserDatabase under key UserDatabase
javax.naming.NameNotFoundException: Name [UserDatabase] is not bound in this Context. Unable to find [UserDatabase].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:819)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:167)
    at org.apache.catalina.realm.UserDatabaseRealm.startInternal(UserDatabaseRealm.java:253)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.realm.CombinedRealm.startInternal(CombinedRealm.java:201)
    at org.apache.catalina.realm.LockOutRealm.startInternal(LockOutRealm.java:120)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1109)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:300)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:731)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)

Mar 21, 2014 7:57:28 AM org.apache.catalina.realm.CombinedRealm startInternal
SEVERE: Failed to start "org.apache.catalina.realm.UserDatabaseRealm/1.0" realm
org.apache.catalina.LifecycleException: Failed to start component [Realm[UserDatabaseRealm]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.realm.CombinedRealm.startInternal(CombinedRealm.java:201)
    at org.apache.catalina.realm.LockOutRealm.startInternal(LockOutRealm.java:120)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1109)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:300)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:731)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
Caused by: org.apache.catalina.LifecycleException: No UserDatabase component found under key UserDatabase
    at org.apache.catalina.realm.UserDatabaseRealm.startInternal(UserDatabaseRealm.java:262)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 17 more

Mar 21, 2014 7:57:31 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/apache-tomcat-7.0.52/webapps/docs
Mar 21, 2014 7:57:32 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/apache-tomcat-7.0.52/webapps/examples
Mar 21, 2014 7:57:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/apache-tomcat-7.0.52/webapps/host-manager
Mar 21, 2014 7:57:33 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/apache-tomcat-7.0.52/webapps/manager
Mar 21, 2014 7:57:34 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 21, 2014 7:57:34 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 21, 2014 7:57:34 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6321 ms

谢谢,LC

3 个答案:

答案 0 :(得分:49)

您需要META-INF/context.xml文件中的ResourceLink才能使全局资源可用于Web应用程序。

 <ResourceLink name="jdbc/mydb"
             global="jdbc/mydb"
              type="javax.sql.DataSource" />

答案 1 :(得分:1)

对于那些使用Tomron和Bitronix的人来说,这将解决问题:

错误表示找不到您的数据源&#39; jdbc / mydb&#39; 的处理程序,因此您需要确保您的tomcat服务器引用您的bitronix根据需要配置文件。

如果您使用 btm-config.properties resources.properties 文件来配置数据源,请在tomcat中指定这两个JVM参数:

(如果您已经使用过它们,请确保您的参考文献是正确的):

  • btm.root
  • bitronix.tm.configuration

e.g。

-Dbtm.root="C:\Program Files\Apache Software Foundation\Tomcat 7.0.59" 
-Dbitronix.tm.configuration="C:\Program Files\Apache Software Foundation\Tomcat 7.0.59\conf\btm-config.properties" 

现在,重新启动服务器并检查日志。

答案 2 :(得分:-1)

您将resource-ref放在web.xml的描述标记中