title: JNDI ClassCastException with tomcat 6.0.26
body: <p>I am developing a webapp that requires setting up a DataSource with JNDI using Enterprise JDBC Classes.I am using the Netbeans 6.9 bundled tomcat (6.0.26 app) server with mysql 5.xx.The issue really is that I can still see the database values from a relation being displayed in my jsp page whereas during the tomcat initialization it says something like this:</p>
  <p>.
  .</p>
  <pre><code>SEVERE: Servlet.service() for servlet jsp threw exception
  java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.naming.Context
          at org.apache.jsp.Hello_jsp._jspService(Hello_jsp.java:141)
          at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
          at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
          at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
          at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
          at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
          at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
          at java.lang.Thread.run(Thread.java:619)
  Jul 25, 2010 1:28:25 AM org.apache.catalina.core.ApplicationContext log
  INFO: SessionListener: contextDestroyed()
  Jul 25, 2010 1:28:25 AM org.apache.catalina.core.ApplicationContext log
  INFO: ContextListener: contextDestroyed()
  Jul 25, 2010 1:28:32 AM org.apache.catalina.core.ApplicationContext log
  INFO: ContextListener: contextInitialized()
  Jul 25, 2010 1:28:32 AM org.apache.catalina.core.ApplicationContext log
  INFO: SessionListener: contextInitialized()
  Jul 25, 2010 1:28:40 AM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet jsp threw exception
  javax.naming.NameNotFoundException: Name nexusirm is not bound in this Context
          at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
          at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
          at javax.naming.InitialContext.lookup(InitialContext.java:392)
          at org.apache.jsp.Hello_jsp._jspService(Hello_jsp.java:141)
          at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
          at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
          at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
          at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
          at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
          at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
          at java.lang.Thread.run(Thread.java:619)
  Jul 25, 2010 1:29:50 AM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet jsp threw exception
  javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
          at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
          at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
          at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
          at javax.naming.InitialContext.lookup(InitialContext.java:392)
          at org.apache.jsp.Hello_jsp._jspService(Hello_jsp.java:143)
          at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
          at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
          at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
          at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
          at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
          at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
          at java.lang.Thread.run(Thread.java:619)
  Jul 25, 2010 1:30:10 AM org.apache.catalina.core.ApplicationContext log
  </code></pre>
  <p>.
  .</p>
  <p>I have the following inside my /WEB-INF/web.xml</p>
  <pre><code>&lt;resource-ref&gt;
           &lt;res-ref-name&gt;jdbc/NexusIRM&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;res-sharing-scope&gt;Shareable&lt;/res-sharing-scope&gt; 
  &lt;/resource-ref&gt;
  </code></pre>
  <p>And the following in my /META-INF/context.xml</p>
  <pre><code> &lt;Resource name=&quot;jdbc/NexusIRM&quot;
             auth=&quot;Container&quot;
             type=&quot;javax.sql.DataSource&quot;
             driverClassName=&quot;com.mysql.jdbc.Driver&quot;
             url=&quot;jdbc:mysql://localhost:3306/nexusirm&quot;
             username=&quot;root&quot;
             password=&quot;&quot;
             maxActive=&quot;20&quot;
             maxIdle=&quot;30&quot;
             maxWait=&quot;-1&quot;
   
   /&gt;
  </code></pre>
  <p>My Application code looks like:</p>
  <pre><code>                &lt;%
   try{
  
  Context initCtx = new InitialContext();
  
  DataSource ds = (DataSource)
    initCtx.lookup(&quot;java:comp/env/jdbc/NexusIRM&quot;);
  
  Connection conn=ds.getConnection();
  
  Statement stmt=conn.createStatement();
  ResultSet rs=stmt.executeQuery(&quot;SELECT * FROM irm_gtresult&quot;);
  
  if(rs.next())
      {
      for(int i=1;i&lt;9;i++){
  out.write(rs.getString(i));
  }
  }
  conn.close();
  }catch(SQLException e)
  {out.write(e.getMessage());
  e.printStackTrace();}
  
  %&gt;
  </code></pre>
  <p>I am just curious to why these exceptions are being thrown while the application is being executed perfectly!</p>