Hi Mike,

I didn't quote your reply, to ensure this tutorial-like thing is as clean as possible.


I assume that you've already downloaded db-derby-10.4.2.0-bin.tar.gz and apache-tomcat-6.0.18.tar.gz *to your home directory*. Every step shown below is somewhere in or below the home directory. For me, this directory on my Mac is '/Users/Stephan'. You should have the default user-rights in this directory. Try not to use spaces or any other strange characters in the path, just to avoid weird circumstances.

I included the terminal-prompt with each command, so you can see in which directory I was when taking these steps. Everything after the dollar sign ($) is the actual command.

We'll start with Derby. Unpack it:

   osx:~ Stephan$ tar xzvf db-derby-10.4.2.0-bin.tar.gz

I omitted the output for brevity, but the directory '/Users/Stephan/db-derby-10.4.2.0-bin' has been created. Let's simply rename it to 'derby' for convenience:

   mv osx:~ Stephan$ mv db-derby-10.4.2.0-bin derby
Switch to the 'derby/bin' directory to edit the startup script.

   osx:~ Stephan$ cd derby/bin
   osx:~/derby/bin Stephan$ pico startNetworkServer
On line 18 you'll find the following:

   # under the License.

   if [ -z "$DERBY_HOME" ]; then

This needs one extra line. Remember, that *MY* Derby was unpacked to /Users/Stephan/Derby. You must change it to yours.

   # under the License.
   export DERBY_HOME=/Users/Stephan/Derby
   if [ -z "$DERBY_HOME" ]; then

Save the file after you made your changes, and repeat this step for the shutdown script 'stopNetworkServer', and the Derby command-line tool 'ij'. They're all in the same directory.

After you also changed the other two scripts, let's start Derby:
osx:~/derby/bin Stephan$ ./startNetworkServer You should see something like this:

   Security manager installed using the Basic server security policy.
   Apache Derby Network Server - 10.4.2.0 - (689064) started and ready
   to accept connections on port 1527 at 2008-12-12
   20:26:37.753 GMT

Next, open a new terminal and start the Derby command line tool:

   osx:~ Stephan$ cd derby/bin
   osx:~ Stephan$ ./ij

Now, we're at the command-prompt of ij, whichs displays : ij>
Everything after the bracket is the actual command. Let's create a database:

ij> CONNECT 'jdbc:derby://localhost/mydatabasename;user=DATABASEUSERNAME;password=DATABASEPASSWORD;create=true';
   ij> DISCONNECT;
   ij> QUIT;

That was Derby. Move on to Tomcat by changing to our home directory:

   osx:~/derby/bin Stephan$ cd ~

Next, unpack tomcat:

   osx:~ Stephan$ tar xzvf apache-tomcat-6.0.18.tar.gz

Again, rename to output directory to 'tomcat' for convenience:

   osx:~ Stephan$ mv apache-tomcat-6.0.18 tomcat

Copy the required Derby libraries to Tomcat:

   osx:~ Stephan$ cp ~/derby/lib/derbyclient.jar ~/tomcat/lib
   osx:~ Stephan$ cp ~/derby/lib/derby.jar ~/tomcat/lib

Remove the ROOT directory and all of its content in tomcat/webapps:

   osx:~ Stephan$ rm -r tomcat/webapps/ROOT

Create a new ROOT directory:

   osx:~ Stephan$ mkdir tomcat/webapps/ROOT
Create a META-INF directory in the ROOT directory;

   osx:~ Stephan$ mkdir tomcat/webapps/ROOT/META-INF
Go to that directory and create the file 'context.xml':

   osx:~ Stephan$ cd tomcat/webapps/ROOT/META-INF
   osx:~ Stephan$ pico context.xml
Paste the following content in the editor, and save the file afterwards. Note that there should be no leading spaces, the file must start with the <?xml declaration:

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
   <Resource name="jdbc/mydatabasename" auth="Container"
       type="javax.sql.DataSource"
       driverClassName="org.apache.derby.jdbc.ClientDriver"
       url="jdbc:derby://localhost:1527/db"
       username="DATABASEUSERNAME"
       password="DATABASEUSERPASSWORD"
   />
</Context>

Return to your home directory:

   osx:~/tomcat/webapps/ROOT/META-INF Stephan$ cd ~

Create a WEB-INF directory in the ROOT directory;

   osx:~ Stephan$ mkdir tomcat/webapps/ROOT/WEB-INF
Go to that directory and create the file 'web.xml':

   osx:~ Stephan$ cd tomcat/webapps/ROOT/WEB-INF
   osx:~ Stephan$ pico web.xml

Paste the following content in the editor, and save the file afterwards.
Note that there should be no leading spaces, the file must start with the <?xml declaration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
   xmlns="http://java.sun.com/xml/ns/javaee";
   xmlns:j2ee="http://java.sun.com/xml/ns/javaee";
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml ns/javaee/web-app_2_5.xsd">
   <display-name>root</display-name>
   <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
       <welcome-file>index.html</welcome-file>
       <welcome-file>index.htm</welcome-file>
       <welcome-file>default.html</welcome-file>
       <welcome-file>default.htm</welcome-file>
       <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
   <resource-ref>
       <description>JNDI www.mydomain.com Connection</description>
       <res-ref-name>jdbc/db</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       <res-sharing-scope>Shareable</res-sharing-scope>
   </resource-ref>
</web-app>

To finish, we need to create the file ROOT/index.jsp:

   osx:~/tomcat/webapps/ROOT/WEB-INF Stephan$ cd ..
   osx:~/tomcat/webapps/ROOT Stephan$ pico index.jsp
Copy the following content in the editor, and save the file afterwards:

<%...@page language="java" import="java.sql.*, javax.sql.*, javax.naming.*" %>
<%
   Context ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/db");
   if (dataSource == null) {
       throw new JspException(
           "java:comp/env/jdbc/db returned NULL");
   }

try {
   Connection conn = dataSource.getConnection();
   out.println("CONNECTED<br>");
   //body of code to go here

}  catch (Throwable e)  {
  out.println(e);
  e.printStackTrace();
} finally {

}

%>

Now let's start tomcat:

   osx:~/tomcat/webapps/ROOT/WebContent Stephan$ cd ~/tomcat/bin
   osx:~/tomcat/bin Stephan$ ./startup.sh
And surf with your browser to http://localhost:8080/

You should see: CONNECTED

Every step was taken and copied in a text-editor, to make sure I didn't forget anything.

Let me know if it works for you.


Regards,

   Stephan.

Reply via email to