Friday, April 18, 2014

JSTL/DataSources in Tomcat 6.0

1, Install the JDBC drivers in TC's lib/ directory.

    Note: JDBC drivers are usually packaged in jar files. For example, derby*.jar for
    Java DB, ojdbc6.jar for Oracle 11g, and sqljdbc4.jar for SQL Server 2008.

2, Add <Resource> in /META-INF/context.xml    Follow the instructions from TC website:

    <Resource name="jdbc/jvdb" auth="Container" type="javax.sql.DataSource"
                    maxActive="100" maxIdle="3" maxWait="10000" username="user1" password=""

3, Add resource-ref in /WEB-INF/web.xml
    For example,

   <resource-ref >
       <description >DB Connection Pool</description >
       <res-ref-name >jdbc/jvdb</res-ref-name >
       <res-type >javax.sql.DataSource </res-type >
       <res-auth >Container</res-auth >
   </resource-ref >

4, Use the datasource in JSTL(v1.2)
    For example, if you are using Derby, you can query Derby's SYSTABLES in a jsp as follows. Of course, we can always change the SQL to query the user-defined tables.

<%@ taglib uri="" prefix="sql" %>   
<%@ taglib uri="" prefix="c" %>   
    <SQL:QUERY var="systables" datasource="jdbc/jvdb">   
       select * from sys.SYSTABLES