title: Name [jdbc/mydb] is not bound in this Context. Unable to find [jdbc]
body: <p>My OS is Windows 10.<br>
  I downloaded Tomcat 9.0 from <a href="https://tomcat.apache.org/download-90.cgi" rel="nofollow noreferrer">https://tomcat.apache.org/download-90.cgi</a><br>
  I downloaded Eclipse from <a href="https://www.eclipse.org/downloads/packages/release/2018-12/r/eclipse-ide-enterprise-java-developers" rel="nofollow noreferrer">https://www.eclipse.org/downloads/packages/release/2018-12/r/eclipse-ide-enterprise-java-developers</a><br>
  I use MySQL 5.5 on an external server.<br>
  I downloaded Connector/J 8.0 from <a href="https://dev.mysql.com/downloads/connector/j/8.0.html" rel="nofollow noreferrer">https://dev.mysql.com/downloads/connector/j/8.0.html</a><br>
  I successfully connected to the MySQL Database with the DriverManager class. </p>
  
  <p>Now I want to use the Tomcat JNDI to establish the database connection but it does not work. <br>
  Tomcat is installed in C:\MyPrograms\Tomcat. <br>
  My Eclipse workspace is C:\Users\Felix\eclipse-workspace. <br></p>
  
  <p>I copied the JDBC driver mysql-connector-java-8.0.13.jar to C:\MyPrograms\Tomcat\lib. <br></p>
  
  <p>My C:\MyPrograms\Tomcat\conf\server.xml (without comments):</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.startup.VersionLoggerListener" /&gt;
    &lt;Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /&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;Resource name="jdbc/mydb" global="jdbc/mydb" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" auth="Container" type="javax.sql.DataSource" username="felix1_0" password="mypassword" driverClassName="com.mysql.jdbc.Driver" description="mydb example" url="jdbc:mysql://192.168.88.88:3306/felix1_0" maxTotal="8" maxIdle="4" maxWaitMillis="10000" removeAbandonedTimeout="300" defaultAutoCommit="true"/&gt;
    &lt;/GlobalNamingResources&gt;
    &lt;Service name="Catalina"&gt;
      &lt;Connector port="9999" protocol="HTTP/1.1"
                  URIEncoding="UTF-8"
                  maxThreads="200"
                  minSpareThreads="10"
                  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;/Host&gt;
      &lt;/Engine&gt;
    &lt;/Service&gt;
  &lt;/Server&gt;
  </code></pre>
  
  <p>my C:\Users\Felix\eclipse-workspace\DynamicWebProjectTest\WebContent\WEB-INF\web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"&gt;
    &lt;display-name&gt;DynamicWebProjectTest&lt;/display-name&gt;
  
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
  
      &lt;resource-ref&gt;
          &lt;description&gt;This is a reference to the global Resource for MySQL database connetion.&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;/web-app&gt;
  </code></pre>
  
  <p>My C:\Users\Felix\eclipse-workspace\DynamicWebProjectTest\WebContent\META-INF\context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE xml&gt;
  &lt;Context&gt;
      &lt;ResourceLink name="jdbc/mydb" global="jdbc/mydb" type="javax.sql.DataSource"/&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>My Servlet:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.Properties;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.sql.DataSource;
  import java.sql.ResultSet;
  
  @WebServlet("/MyServlet")
  public class MyServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      public MyServlet() {
          super();
      }
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("&lt;html&gt;&lt;body&gt;");
          out.println("&lt;h3&gt;Hello Eclipse&lt;/h3&gt;&lt;br&gt;&lt;br&gt;");
          out.println("&lt;p&gt;A Random Number: &lt;strong&gt;" + Math.random() + "&lt;/strong&gt;&lt;/p&gt;");
  
          try {
              establishJNDIDatabaseConnection(out);
          } catch (NamingException e) {
              out.println("&lt;br&gt;Error: NamingException: " + e.getMessage());
          } catch (SQLException e) {
              out.println("&lt;br&gt;Error: SQLException: " + e.getMessage());
          }
          out.println("&lt;/body&gt;&lt;/html&gt;");
      }
  
      void establishJNDIDatabaseConnection(PrintWriter out) throws NamingException, SQLException {
          /*
           * Get initial context that has references to all configurations and 
           * resources defined for this web application. 
           */
          Context initialContext = new InitialContext();
          out.println("&lt;br&gt;Context initialContext = new InitialContext();");
  
          /*
           * Get Context object for all environment naming (JNDI), such as 
           * Resources configured for this web application. 
           */
          Context environmentContext = (Context) initialContext.lookup("java:comp/env");
          out.println("&lt;br&gt;Context environmentContext = (Context) initialContext.lookup(\"java:comp/env\");");
  
          /*
           * Get the DataSource for MySQL to request a connection
           */
          DataSource dataSource = (DataSource) environmentContext.lookup("jdbc/mydb");
          out.println("&lt;br&gt;DataSource dataSource = (DataSource) environmentContext.lookup(\"jdbc/mydb\");");
  
          /*
           * Request a Connection from the pool of connection threads
           */
          Connection con = dataSource.getConnection();
          out.println("&lt;br&gt;Connection con = dataSource.getConnection();");
  
          /*
           * Query the database
           */
          StringBuilder msg = new StringBuilder();
          try (Statement stm = con.createStatement()) {
              ResultSet rs = stm.executeQuery("SHOW TABLES;");
              while(rs.next()) {
                  msg.append(rs.getString("Tables_in_felix1_0"));
              }
          } catch(SQLException e) {
              out.println("&lt;br&gt;Error: " + e.getMessage());
          } finally {
              if(con != null) {
                  con.close();
              }
              con = null; //prevent future access
          }
  
          out.println("&lt;br&gt;Returned from SQL: " + msg.toString());
      }
  }
  </code></pre>
  
  <p>The Error: "NamingException: Name [jdbc/mydb] is not bound in this Context. Unable to find [jdbc]." is thrown by this line of code:</p>
  
  <pre><code>DataSource dataSource = (DataSource) environmentContext.lookup("jdbc/mydb");
  </code></pre>
  
  <p>How can I fix this problem?</p>