You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jason Ling <ja...@gmail.com> on 2008/02/13 22:40:21 UTC

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!

Re: DataSource binding for JSP

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

Jason Ling wrote:
| the JDBC driver could not be found.  I made sure the the jdbc jar
| file was in place and rebooted the computer, and the same jsp file
| worked.

I'm glad you have solved your problem (though rebooting should not have
been necessary!).

| I am actually learning servlet and jsp.  The jsp file is an exercise
| from a book but it uses the driver manager method (the forName()
| method) which I changed to the DataSource method.

Sounds good. Just remember that book examples often omit details like
proper exception handling and resource management because print all of
that stuff costs money and distracts the reader. I would encourage you
to analyze the code you are reading when you write such routines "for
real" and make sure that you are doing things like closing all resources
in finally blocks and things like that. Also, a "swallowed" exception
(one that is caught and ignored completely) is a serious no-no: you
should at least log the error in one way or another.

Best of luck learning servlets and Java in general.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAke0c5IACgkQ9CaO5/Lv0PDH8wCgsTsXU48pAZ5uY79z1WcoIhON
8wMAn3Bc7ESq5gTcisH5wj0vPrRt2RCW
=J27b
-----END PGP SIGNATURE-----

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


Re: DataSource binding for JSP

Posted by Jason Ling <ja...@gmail.com>.
Chris,

Thanks for the notes.  It turned out the problem (data not retrieved from
the database) was not caused by the code, but by the tomcat server.  I
invoked the same jsp file and there was one more problem that the JDBC
driver could not be found.  I made sure the the jdbc jar file was in place
and rebooted the computer, and the same jsp file worked.

I am actually learning servlet and jsp.  The jsp file is an exercise from a
book but it uses the driver manager method (the forName() method) which I
changed to the DataSource method.

Your notes gave me the assurance that the DataSource declaration in
context.xml and the reference the the deployment descriptor are used by both
servlet files alike, whether it is written by us or translated from jsp.

Thanks again.

Jason



On Wed, Feb 13, 2008 at 6:33 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Jason,
>
> Jason Ling wrote:
> | 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.
>
> [snip]
>
> | <%! DataSource ds; %>
>
> This is dangerous; you have a DataSource member, rather than a local.
> You should change this by removing the "!" so that this is a local
> variable. If you don't do this, you will run into threading problems.
>
> | <%
> |   try {
> |      Context initCtx = new InitialContext();
> |      Context envCtx = (Context) initCtx.lookup("java:comp/env");
> |      ds = (DataSource) envCtx.lookup("jdbc/DBDevTrackConnDS");
> |   } catch (NamingException e) {
> |      e.printStackTrace();
> |   }
> | %>
>
> Why do you allow processing to continue if there is an exception getting
> the DataSource? There is nothing useful your JSP can do if the
> DataSource is not successfully obtained. My guess is that you are
> getting a NullPointerException below...
>
> [snip]
>
> |   try {
> [snip]
> |     }
> [snip]
> |   }
> |   catch (SQLException e) {
> |   }
> |   catch (Exception e) {
> |   }
>
> ... which you can't see because you are swallowing all exceptions.
>
> | javax.naming.NameNotFoundException: Name jdbc is not bound in this
> Context
>
> This is another problem that is definitely the cause of your problems.
> It's good that you had this problem, so you'd ask a question and see all
> of the /other/ problems that you didn't know you had in the first place.
>
> | I have been using the same try{} blocks in my servlet files.
>
> Your try/catch idiom leaks connections, statements, and result sets. :(
> You should fix that, too.
>
> | 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:
>
> Why would you think that? Are you running your servlets and JSPs in
> different webapps? If not, then the context.xml file specifies setup for
> all servlets in a webapp (including those translated from JSP).
>
> - -chris
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.8 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkezfj0ACgkQ9CaO5/Lv0PBjIwCZAVMjTBygZj1iDvCNQY9hWKka
> xrMAn0/uF81TI4V1ydvJ+s1rbLndVAb9
> =Sn3x
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: DataSource binding for JSP

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jason,

Jason Ling wrote:
| 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.

[snip]

| <%! DataSource ds; %>

This is dangerous; you have a DataSource member, rather than a local.
You should change this by removing the "!" so that this is a local
variable. If you don't do this, you will run into threading problems.

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

Why do you allow processing to continue if there is an exception getting
the DataSource? There is nothing useful your JSP can do if the
DataSource is not successfully obtained. My guess is that you are
getting a NullPointerException below...

[snip]

|   try {
[snip]
|     }
[snip]
|   }
|   catch (SQLException e) {
|   }
|   catch (Exception e) {
|   }

... which you can't see because you are swallowing all exceptions.

| javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

This is another problem that is definitely the cause of your problems.
It's good that you had this problem, so you'd ask a question and see all
of the /other/ problems that you didn't know you had in the first place.

| I have been using the same try{} blocks in my servlet files.

Your try/catch idiom leaks connections, statements, and result sets. :(
You should fix that, too.

| 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:

Why would you think that? Are you running your servlets and JSPs in
different webapps? If not, then the context.xml file specifies setup for
all servlets in a webapp (including those translated from JSP).

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkezfj0ACgkQ9CaO5/Lv0PBjIwCZAVMjTBygZj1iDvCNQY9hWKka
xrMAn0/uF81TI4V1ydvJ+s1rbLndVAb9
=Sn3x
-----END PGP SIGNATURE-----

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