JNDI

From Ingres Community Wiki

Jump to: navigation, search

Contents

JDBC and JNDI

The sample code from the article Accessing data from JSP includes the connection parameters and credentials as part of the JSP page.

Using the same schema and data the following article uses the JavaServer Pages Standard Tag Library (JSTL) to generate the same XML fragment. The JSTL is an extension to tomcat and should be downloaded and installed. JSTL 1.1 requires a JSP container of 2.0 or later and this is included in Tomcat 5.x and later. It also requires J2SE 1.4.2 or later which already includes many of its dependencies. In this example we're using tomcat 5.0.28 or 6.0 and installing the JSTL jar files into the applications CLASSPATH directory. For more information take a look at Getting Started with the Standard Tag Library.

JSTL jar files located in WEB-INF/lib

jstl.jar
standard.jar

Having spent a while investigating various Tomcat server configurations I eventually found the cause of my errors and have decided to dedicate a section to each Tomcat server.

Tomcat

The Tomcat server includes configurations that covers different scopes and determining which configuration to change seems to be a recurrent theme in many Web posts.

Typically, a Web application has two configuration files that control deployment and context.

WEB-INF/web.xml
Web application deployment descriptor
META-INF/context.xml
Context container

Configuration is stored under ${CATALINA_HOME}/webapps/application/META-INF and ${CATALINA_HOME}/webapps/application/WEB-INF.

Tomcat has its own web.xml located in the ${CATALINA_HOME}/conf directory and defines the default values for all applications. In earlier versions of tomcat the server.xml contained <context></context> sections.

When an application is deployed using a .war file the META-INF/context.xml is moved to ${CATALINA_HOME}/conf/enginename/hostname contains the application.xml. The file can be manually moved into the ${CATALINA_HOME}/conf/enginename/hostname for any application ensuring that the filename of the file matches the application directory name in the webapps directory.

Path and filenames
Variable Value
enginename Catalina
hostname localhost
application ingdb

Customise the resource parameter values for the database connection.

5.0.x

  • ${CATALINA_HOME}/conf/Catalina/localhost/ingdb.xml or ${CATALINA_HOME}/webapps/ingdb/META-INF/context.xml
<Context path="/ingdb"
    debug="5" reloadable="true" crossContext="true">
	<Resource name="jdbc/LaszloAmazonDB" auth="Container" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/LaszloAmazonDB">
        <parameter>
          <name>driverClassName</name>
          <value>com.ingres.jdbc.IngresDriver</value>
        </parameter>
        <parameter>
          <name>url</name>
          <value>jdbc:ingres://localhost:II7/laszlodb</value>
        </parameter>
        <parameter>
          <name>username</name>
          <value>tomcat</value>
        </parameter>
        <parameter>
          <name>password</name>
          <value>tomcat</value>
        </parameter>
    </ResourceParams>
</Context>

It seems that in this version of tomcat the format of the resource parameters must be in long form, otherwise the following error is returned Unable to get connection, DataSource invalid: "org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class ' ' for connect URL 'null', cause: null"This error is also returned if the iijdbc.jar file cannot be located anywhere in the CLASSPATH.

It should also be noted that the content of the <value></value> tags can contain a subset of characters and I couldn't find a way to escape the dollar '$' character. In the end I changed the user's password to contain only alpha-numerics.

  • ${CATALINA_HOME}/webapps/ingdb/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
    version="2.4">

  <display-name>Ingres Test</display-name>
  <description>
     Welcome to Ingres Test
  </description>

  <resource-ref>
    <description>Laszlo Demo DB</description>
    <res-ref-name>jdbc/LaszloAmazonDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

5.5.x and 6

  • ${CATALINA_HOME}/conf/Catalina/localhost/ingdb.xml or ${CATALINA_HOME}/webapps/ingdb/META-INF/context.xml
<Context path="/ingdb"
    debug="5" reloadable="true" crossContext="true">
	<Resource name="jdbc/LaszloAmazonDB" auth="Container" type="javax.sql.DataSource"
		maxActive="100" maxIdle="30" maxWait="10000"
        	username="tomcat" password="tomcat" driverClassName="com.ingres.jdbc.IngresDriver"
        	url="jdbc:ingres://localhost:II7/laszlodb"/>
</Context>
  • ${CATALINA_HOME}/webapps/ingdb/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
    version="2.4">
  <display-name>Ingres Test</display-name>
  <description>
     Welcome to Ingres Test
  </description>

  <resource-ref>
    <description>Laszlo Demo DB</description>
    <res-ref-name>jdbc/LaszloAmazonDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

Tomcat is now ready for a JSTL page that uses the standard and Database Connection Pooling (DBCP) tag libraries.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<sql:setDataSource dataSource="jdbc/LaszloAmazonDB"/>
<addresslist> <sql:query var="address">
    SELECT address_fullname, address, city, state, zip
    FROM addresslist
</sql:query>
<c:forEach var="row" items="${address.rows}">
<address fullname="${row.address_fullname}"
    address="${row.address}"
    city="${row.city}"
    state="${row.state}"
    zip="${row.zip}"/>
</c:forEach>
</addresslist>

The dataSource references the resource configured in web.xml which in turn resolves to context.xml.

The query results are referenced by the name address defined as the value of var and the resulting fragment is constructed by iterating through the results.

Test it

Viewing the page http://localhost:8080/ingdb/getaddress.jsp returns the following XML fragment.

<addresslist> 
<address fullname="Laszlo"
    address="1040 Mariposa Street"
    city="San Francisco"
    state="CA"
    zip="94107"/>
<address fullname="Emma McGrattan"
    address="2950 Express Drive South"
    city="Islandia"
    state="NY"
    zip="11749"/>
<address fullname="Ingres Corporation"
    address="2950 Express Drive South"
    city="Islandia"
    state="NY"
    zip="11749"/>
</addresslist>
Personal tools
Developing With