title: Can't access object using Java JNDI context lookup
body: <p>I'm running Tomcat6 and want to access the datasource from my Servlet.  But I'm getting </p>
  
  <pre><code>    javax.naming.OperationNotSupportedException: can''t generate an absolute name for this namespace
  at org.apache.naming.NamingContext.getNameInNamespace(NamingContext.java
  :772)
  </code></pre>
  
  <p>My context.xml is under HomeController/META-INF/context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Context path="/HomeController"&gt; 
  &lt;Resource name="jdbc/HomeController" auth="Container" type="javax.sql.DataSource" driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"   
  acquireIncrement="5"   
   username="client" 
  password="1234" 
  jdbcUrl="jdbc:sqlserver://192.168.1.5:1433;databaseName=myDB;autoReconnect=true" /&gt; 
  &lt;/Context&gt;
  </code></pre>
  
  <p>I also have the same context.xml renamed to my webapp "HomeController" and placed under TOMCAT_HOME/conf/Catalina/localhost/HomeController.xml.</p>
  
  <p>My web.xml looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
   &lt;display-name&gt;Home Controller&lt;/display-name&gt;
   &lt;servlet&gt; 
    &lt;servlet-name&gt;HomeController&lt;/servlet-name&gt; 
    &lt;servlet-class&gt;com.home.controller.HomeController&lt;/servlet-class&gt; 
  
   &lt;/servlet&gt; 
  
   &lt;servlet-mapping&gt; 
    &lt;servlet-name&gt;HomeController&lt;/servlet-name&gt; 
    &lt;url-pattern&gt;/HomeController&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt; 
  
     &lt;resource-ref&gt;
          &lt;description&gt;DB Connection&lt;/description&gt;
          &lt;res-ref-name&gt;jdbc/HomeController&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>In my Servlet I have this:</p>
  
  <pre><code>Connection conn = null;
           Context ctx = null;
           java.sql.Statement stmt = null;
  
           try {
               ctx = new InitialContext();
  
               Context envCtx = (Context) ctx.lookup("java:comp/env");  //&lt;--FAILS HERE
  
               DataSource ds = (DataSource) envCtx.lookup("jdbc/EmscribeWS");
  
               conn = ds.getConnection();
               DatabaseMetaData mt = conn.getMetaData();                                                 
               stmt = conn.createStatement();                 
  
       } catch (Exception e) {
        e.printStackTrace();         
       }
  </code></pre>
  
  <p>I installed a fresh copy of Tomcat6 and placed the sqljdbc4.jar under TOMCAT_HOME/lib.
  I have no JARS under my HomeController/WEB-INF/lib.</p>
  
  <p>It fails when executing  "(Context) ctx.lookup("java:comp/env"); " in my Servlet.</p>
  
  <p>Anyone knows why it fails when retrieving object? </p>