title: Name [jdbc/mydb] is not bound in this Context
body: <p>I see this question was raised several times already and I went through all of them. But I am still unable to fix my problem.</p>
  
  <p>Could anyone help me pinpoint what I am doing wrong?</p>
  
  <p>I get the following error message when I try to access database.jsp:</p>
  
  <pre><code>    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:         &lt;%
  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)
  </code></pre>
  
  <p>Here are a few pieces of information on my installation:
  My computer is running on Mac OS X. The web server is Tomcat version 7.0.52, installed under /Users/username/Dropbox/Server/apache-tomcat-7.0.52. I am trying to run Derby 10.10.1.1 as an embedded database. I copied derby.jar under the folder /Users/username/Dropbox/Server/apache-tomcat-7.0.52/lib. I wrote the following in my PATH file (I am not sure it is relevant):</p>
  
  <pre><code>export DERBY_HOME=/Users/username/Dropbox/Server/apache-tomcat-7.0.52/lib
  export PATH=$PATH:$DERBY_HOME/bin
  </code></pre>
  
  <p>Here is the content of the web.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;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"&gt;
  
    &lt;display-name&gt;Welcome to Tomcat&lt;/display-name&gt;
    &lt;description&gt;
       Welcome to Tomcat
      &lt;resource-ref&gt;
         &lt;description&gt;DB Connection&lt;/description&gt;
         &lt;res-ref-name&gt;jdbc/mydb&lt;/res-ref-name&gt;
         &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
         &lt;res-auth&gt;Container&lt;/res-auth&gt;
      &lt;/resource-ref&gt;
    &lt;/description&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>context.xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!-- The contents of this file will be loaded for each web application --&gt;
  &lt;Context&gt;
      &lt;!-- Default set of monitored resources --&gt;
      &lt;WatchedResource&gt;WEB-INF/web.xml&lt;/WatchedResource&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>server.xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;Server port="8005" shutdown="SHUTDOWN"&gt;
    &lt;Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /&gt;
    &lt;Listener className="org.apache.catalina.core.JasperListener" /&gt;
    &lt;Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /&gt;
    &lt;Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /&gt;
    &lt;Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /&gt;
  
    &lt;GlobalNamingResources&gt;
          &lt;!-- Added by LC #2 @19/3/2014 --&gt;
              &lt;!-- Global Datasource for Derby mydb database --&gt;
              &lt;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"/&gt;
          &lt;!-- End #2 --&gt;
  
    &lt;/GlobalNamingResources&gt;
  
    &lt;Service name="Catalina"&gt;
  
      &lt;Connector port="8080" protocol="HTTP/1.1"
                 connectionTimeout="20000"
                 redirectPort="8443" /&gt;
  
      &lt;Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /&gt;
  
      &lt;Engine name="Catalina" defaultHost="localhost"&gt;
        &lt;Realm className="org.apache.catalina.realm.LockOutRealm"&gt;
          &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                 resourceName="UserDatabase"/&gt;
        &lt;/Realm&gt;
  
        &lt;Host name="localhost"  appBase="webapps"
              unpackWARs="true" autoDeploy="true"&gt;
  
          &lt;Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                 prefix="localhost_access_log." suffix=".txt"
                 pattern="%h %l %u %t &amp;quot;%r&amp;quot; %s %b" /&gt;
  
          &lt;!-- Added by LC #1 @19/3/2014 --&gt;
          &lt;Context
             docBase="ROOT" path="" debug="0"
             reloadable="true"&gt;
          &lt;/Context&gt;
          &lt;!-- End #1 --&gt;
  
        &lt;/Host&gt;
      &lt;/Engine&gt;
    &lt;/Service&gt;
  &lt;/Server&gt;
  </code></pre>
  
  <p>Finally, here is the database.jsp page:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Quote Query&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;retrieve stock quotes&lt;/h1&gt;
    &lt;h3&gt;Choose Ticker(s):&lt;/h3&gt;
    &lt;form method="get"&gt;
      &lt;input type="checkbox" name="ticker" value="AAPL"&gt;AAPL
      &lt;input type="checkbox" name="ticker" value="MSFT"&gt;MSFT
      &lt;input type="submit" value="Query"&gt;
    &lt;/form&gt;
  
    &lt;%
      String[] tickers = request.getParameterValues("ticker");
      if (tickers != null) {
    %&gt;
  
  
          &lt;%@ page import="javax.naming.*,java.sql.*,javax.sql.*" %&gt;
          &lt;%
                  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 &lt; 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);
          %&gt;
  
        &lt;hr&gt;
        &lt;form method="get" action="quotes.jsp"&gt;
          &lt;table border=1 cellpadding=5&gt;
            &lt;tr&gt;
              &lt;th&gt;Ticker&lt;/th&gt;
              &lt;th&gt;Date&lt;/th&gt;
              &lt;th&gt;PriceOpen&lt;/th&gt;
              &lt;th&gt;PriceHigh&lt;/th&gt;
              &lt;th&gt;PriceLow&lt;/th&gt;
              &lt;th&gt;PriceClose&lt;/th&gt;
              &lt;th&gt;Volume&lt;/th&gt;
              &lt;th&gt;PriceAdjClose&lt;/th&gt;
            &lt;/tr&gt;
    &lt;%
        while (rset.next()) {
          int id = rset.getInt("id");
    %&gt;
            &lt;tr&gt;
              &lt;td&gt;&lt;input type="checkbox" name="id" value="&lt;%= id %&gt;"&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= rset.getString("Ticker") %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= rset.getString("Date") %&gt;&lt;/td&gt;
              &lt;td&gt;$&lt;%= rset.getInt("PriceOpen") %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= rset.getInt("PriceHigh") %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= rset.getString("PriceLow") %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= rset.getString("PriceClose") %&gt;&lt;/td&gt;
              &lt;td&gt;$&lt;%= rset.getInt("Volume") %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= rset.getInt("PriceAdjClose") %&gt;&lt;/td&gt;
            &lt;/tr&gt;
    &lt;%
        }
    %&gt;
          &lt;/table&gt;
          &lt;br&gt;
          &lt;input type="submit" value="Order"&gt;
          &lt;input type="reset" value="Clear"&gt;
        &lt;/form&gt;
        &lt;a href="&lt;%= request.getRequestURI() %&gt;"&gt;&lt;h3&gt;Back&lt;/h3&gt;&lt;/a&gt;
    &lt;%
        rset.close();
        stmt.close();
        conn.close();
      }
    %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Log</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Thanks, LC</p>