29.10.06

Configuring Jetty 6 to work with Oracle DB

I had the need to print out certain things from my OracleXE database + setup a Web Services engine. My Linux box was running only on 256MB of RAM so I needed a small servlet engine to acts as application server for my JSP application. There were two main options I considered:

  1. Tomcat

  2. Jetty



Tomcat



Started with Tomcat 5.5. Installed Tomcat. Installed Axis 1.4. For Axis I needed to manually install bunch of missing JARs. Tried a simple JSP page with JSLT and SQL tags to dig out information from OracleXE. Just to get into phase where I didn't get any missing class exceptions took me a while. I had to manually install mail.jar, activation.jar, xmlsec.jar etc. In the end of stripping out the exceptions I finally could connect the OracleXE database but the only problem was that none of the results were not displayed in the actual JSP result HTML (despite the connection to the database was working). Weird... I used quite a bit of time searching internet for answer how to get this working.

It was time to throw the towel to the ring. Farewell Tomcat.

Jetty



Downloaded Jetty 6.0.1. Installed it. Installed Axis 1.4. Axis installation was a nice surprise, almost all the needed libraries were already there for Axis 1.4. I only needed to add xmlsec jar file and Axis was happy. Axis installation was much more fluent in Jetty than in Tomcat. Tried my test JSP to connect OracleXE. There I faced some challeges but got over them very fast and was up and running with working JSP, using JSLT + SQL tags printing results out of my Oracle database.

Jetty was a pleasant surprise to me. For those folks that needs sample application that works with Oracle database using data sources, here are the steps to do it:

1. Create directory webapps-plus under $JETTY_HOME if that directory doesn't exists.
Jetty 6.0.1 is configured out-of-the-box with JNDI support if you deploy your application under webapps-plus directory. Also in order to get the JNDI support you need to start the Jetty engine with following parameters:

java -jar start.jar etc/jetty.xml etc/jetty-plus.xml



2. Create a sample application subdirectory $JETTY_HOME/webapps-plus/mysample.
3. Copy WEB-INF from $JETTY_HOME/webapps/test to $JETTY_HOME/webapps-plus/mysample
4. Copy Oracle JDBC driver to $JETTY_HOME/webapps-plus/mysample/WEB-INF/lib. I use ojdbc14.jar.
5. Edit the jetty-web.xml in $JETTY_HOME/webapps-plus/mysample/WEB-INF

You can configure the Oracle datasource in this config file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://j
etty.mortbay.org/configure.dtd">

<Configure class="org.mortbay.jetty.webapp.WebAppContext">

<Call class="org.mortbay.log.Log" name="debug">
<Arg>executing jetty-web.xml</Arg>
</Call>

<!-- Add a DataSource only valid for this webapp -->
<New id="oraclexe" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>jdbc/oraclexe</Arg>
<Arg>
<New class="oracle.jdbc.pool.OracleDataSource">
<Set name="user">scott</Set>
<Set name="password">tiger</Set>
<Set name="URL">jdbc:oracle:thin:@myhostname.com:1521:XE</Set>
</New>
</Arg>
</New>
</Configure>


6. Create a sample JSP file that reports data from Oracle database using the data source defined in jetty-web.xml:

Here is the sample JSP with SQL tags:

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
<title>Oracle DB Test</title>
</head>
<body>

<h2>Results</h2>
<sql:query dataSource="jdbc/oraclexe" var="rs">
SELECT * FROM dual
</sql:query>

<c:choose>
<c:when test="${rs.rowCount == 0}">
Sorry, no messages found.
</c:when>
<c:when test="${rs.rowCount == null}">
Sorry, no messages found (null).
</c:when>
<c:otherwise>
Messages found (${rs.rowCount})
</c:otherwise>
</c:choose>

<c:forEach var="row" items="${rs.rows}">
Dummy ${row.dummy}<br/>
</c:forEach>

</body>
</html>


7. Bounce jetty to get all the changes in effect.

If everythings went well, you should be having a working connection to Oracle database printing out something like:
Results
Messages found (1) Dummy X

1 comment:

javastuff said...

Great Post!!!
Saved me a lot of time and effort.
Jetty Rocks!