David Botterill's Weblog

« Previous day (Aug 8, 2006) | Main | Next day (Aug 10, 2006) »

20060809 Wednesday August 09, 2006

Defining Java Studio Creator 2 Data Sources in Tomcat


I'm sure this will be old information to some people.  But, I had the need to look at a Java Studio Creator 2 web application in Tomcat 5.5 the other day and I needed to define the sample Travel database in JavaDB to the Tomcat container.  There is a good help topic on this in Java Studio Creator 2 than can easily be found by searching on "tomcat".  The first hit is "Deployment Example: Tomcat".  This will give you most of the necessary information.  In this blog I'll give you some critical pieces to allow you to talk to the sample database in JavaDB bundled with Java Studio Creator 2.  Yes this applies to Java Studio Creator 2 Update 1 as well.

Copying the JavaDB JDBC Jar File

First, you will need to copy the JavaDB JDBC Jar file to the Tomcat container so Tomcat can load the JDBC driver when needed.  To do this, copy

creator_install_dir/SunAppserver8/derby/lib/derbyclient.jar

to

tomcat_install_dir/common/lib.

Add a "context.xml" to Your Project

Next you will need to define the data resource to the Tomcat container.  There are really two places a JNDI (Java Naming and Directory Interface) data resource needs to be defined.  The first place is in the application itself.  This definition is taken care of by Java Studio Creator in the "web.xml" file via the "<resource-ref>" element.  The other definition needs to be in the container.  In Tomcat, this can be in the "server.xml" file which will be applicable for every web application or in a "context.xml" in the web application's "META-INF" directory.  

Here are the steps to create the "context.xml" file for your Java Studio Creator 2 web application.

1. Add a directory called "META-INF" to your project by selecting the "File" window, then the "web" directory.  Right-click on the "web" directory and select "New->Folder".  When the dialog comes up, name the folder "META-INF" and hit the "Finish" button.

add META-INF

2. Add a "context.xml" file to the "META-INF" directory by selecting the "File" window, then the "web/META-INF" directory.  Right-click on the "web/META-INF" directory and select "New->File/Folder...".  When the "New File" dialog appears, select "Resources->XML Document". Select "Next" and name the XML Document "context.xml".  Select "Next" and choose "Well-formed Document".  Press "Finish".  When the "context.xml" file appears in the editor, replace the contents with the following and save the file.

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/CorporateTravelCenter" docBase="CorporateTravelCenter"
        debug="5" reloadable="true" crossContext="true">
  <Resource name="jdbc/Travel" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="travel" password="travel" driverClassName="org.apache.derby.jdbc.ClientDriver"
               url="jdbc:derby://localhost:21527/sample"/>
</Context>
 
Lastly, you'll want to "clean and build" your project.  Now when you create a WAR file, the context.xml file will be part of it and used by Tomcat to setup the data resource.
Posted by david ( Aug 09 2006, 05:55:52 PM MDT ) Permalink Comments [7] del.icio.us | digg | technorati