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.