Sunday, January 20, 2008

Java Server Pages with CDK functionality

Setting up interactive web pages can be done in many way. Java Server Pages are just one of them. They are quite similar to PHP pages or Ruby, and combine plain HTML (and likely any other output) code with fragments of code; Java source code in this case.

Ubuntu's tomcat5.5 package installs quite easily, and sets up a server at port 8180. I still have to figure out how to nicely integrate it with the Apache server on port 80, though. Suggestions much appreciated.

From then on, one can add new JSP pages by creating a 'webapp' in /usr/share/tomcat5.5-webapps. The basic structure looks like:
Just copying the large CDK jar (the one with all the third party libraries) into WEB-INF/lib/ did not work for me, but unjaring it into WEB-INF/classes/ seem to work fine.

Then, you can just add Java code using the CDK library for what ever you like. The following (simple) example JSP page, takes one parameter, a molecular formula. This could be the input given in a FORM, but the below page does not deal with that situation yet:
<%@ page import="java.util.*,org.openscience.cdk.*,*" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
String mf = request.getParameter( "mf" );
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="E.L. Willighagen">
<title>Metabolomics Examples</title>

<body bgcolor="#FFFFFF">

<td>Molecular Formula:</td>
<td><%= mf %></td>

MFAnalyser analyser = new MFAnalyser(mf, new Molecule());
double accurateMass = Math.round(analyser.getMass()*10000.0)/10000.0;

<td>Mono-isotopic Accurate Mass:</td>
<td><%= accurateMass %></td>

Now, a lot of improvement can be achieved. For example, the <head> stuff can be split out in a header.include. And, after proper integration with the Apache server, rewrites could be used to create a REST service. But, the above is just to give you an idea.

In case you wonder, this work is related to the opensource MetWare database software development our group is involved in.


  1. Regarding Apache and Tomcat might provide some useful hints (though it's focused on web servcies)

  2. Rajarshi, thanx! That indeed sounds like the hints I am looking for.

    BTW, I forgot to mention to make a symbolic link in /usr/share/tomcat5.5-webapps.

  3. J2EE servers consist of two parts, EJB container and Web Container. EJB containers provide a runtime environment to the Enterprise Java Bean components holding the business logic, while the web containers hold the presentation logic in the form of servlets and Java server pages.