What is this? 

<%! DataSource ds; %>

Do you need that on your page? I never run such a reference on mine. I've also 
imported the following packages -

<%@ page 
import="javax.naming.Context,javax.naming.InitialContext,javax.naming.NamingException,javax.sql.DataSource"
 %>

My JSP config looks like:
----------------------------------------
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/myoracle");
// Allocate and use a connection from the pool
Connection connection = ds.getConnection();


-----------------
and do you have this "DBDevTrackConnDS" set up in your server.xml file -- 
either in the context tag or your respective META-INF directory's .xml files
?

-----Original Message-----
From: Jason Ling [mailto:[EMAIL PROTECTED]
Sent: Wednesday, February 13, 2008 3:40 PM
To: users@tomcat.apache.org
Subject: DataSource binding for JSP


I have the following jsp file on tomcat 5.5.23.  When the file is invoked,
it only returns the header row of the table (First Name, Last Name, User
Name, Password), but does not return the expected records from the Oracle
data table.  The entire jsp file is as follows:

<%@ page session="false" %>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*, java.io.*,
java.util.*" %>

<%! DataSource ds; %>

<%
  try {
     Context initCtx = new InitialContext();
     Context envCtx = (Context) initCtx.lookup("java:comp/env");
     ds = (DataSource) envCtx.lookup("jdbc/DBDevTrackConnDS");
  } catch (NamingException e) {
     e.printStackTrace();
  }
%>

<HTML>
<HEAD>
<TITLE>Display All Users</TITLE>
</HEAD>
<BODY>
<CENTER>
<BR><H2>Displaying All Users</H2>
<BR>
<BR>
<TABLE border="1" cellspacing="0">
<TR>
<TH>First Name</TH>
<TH>Last Name</TH>
<TH>User Name</TH>
<TH>Password</TH>
</TR>

<%
  String sql = "SELECT FirstName, LastName, UserName, Password FROM
tomcats";
  try {
    Connection con = ds.getConnection();

    Statement s = con.createStatement();
    ResultSet rs = s.executeQuery(sql);

    while (rs.next()) {
      out.println("<TR>");
      out.println("<TD>" + rs.getString(1) + "</TD>");
      out.println("<TD>" + rs.getString(2) + "</TD>");
      out.println("<TD>" + rs.getString(3) + "</TD>");
      out.println("<TD>" + rs.getString(4) + "</TD>");
      out.println("</TR>");
    }
    rs.close();
    s.close();
    con.close();
  }
  catch (SQLException e) {
  }
  catch (Exception e) {
  }
%>

</TABLE>
</CENTER>
</BODY>
</HTML>

Checking the log file, the stdout file has the following:

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.jsp.jsp.dblogin_jsp._jspService(dblogin_jsp.java:55)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.jasper.servlet.JspServletWrapper.service(
JspServletWrapper.java:328)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java
:315)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(
ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(
StandardWrapperValve.java:210)
    at org.apache.catalina.core.StandardContextValve.invoke(
StandardContextValve.java:174)
    at org.apache.catalina.core.StandardHostValve.invoke(
StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(
ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(
StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(
CoyoteAdapter.java:151)
    at org.apache.coyote.http11.Http11AprProcessor.process(
Http11AprProcessor.java:834)
    at
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(
Http11AprProtocol.java:640)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java
:1286)
    at java.lang.Thread.run(Unknown Source)

I have been using the same try{} blocks in my servlet files.  The connection
to the same database has always be successful and the data records are
retrieved.  I figure that the only difference between the servlets and the
jsp is that the servlet has the binding information for the database in the
appName/META-INF/context.xml file:

<Context>
    <Resource  name="jdbc/DBDevTrackConnDS"
               auth="Container"
               type="javax.sql.DataSource"
               username="testUser"
               password="testPW"
               driverClassName="oracle.jdbc.driver.OracleDriver"
               url="jdbc:oracle:thin:@sb.lehman.cuny.edu:1521:idm0"
               maxActive="8"
               maxIdle="4" />
</Context>

and the reference information in the appName/WEB-INF/web.xml file:

<web-app>
   <resource-ref>
      <description>
         Resource refrence to a factory for java.sql.Connection instnaces
         that may be used for talking to a particular database that is
         configured in the server.xml file.
      </description>
      <res-ref-name>
         jdbc/DBDevTrackConnDS
      </res-ref-name>
      <res-type>
         javax.sql.DataSource
      </res-type>
      <res-auth>
         Container
      </res-auth>
   </resource-ref>

What is wrong or missing with the jsp file?  Apparently the jsp file is not
aware of the context.xml file and the web.xml file.  How do I convey the
same binding and reference information to the jsp container and the jsp
page?

Many thanks for helping me out!

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to