Monday, October 15, 2007

OpenLaszlo with Java Servlets 2.5 and Resource Injection

OpenLaszlo provides a compelling solution for building highly flexible, rich, and robust web clients that are supported on the back end by business logic and data tier implemented with Java EE (servlets and EJB3/JPA for example). OpenLaszlo supports HTTP access of the back-end servlets and the OpenLaszlo servlet download makes it very simple to incorporate OpenLaszlo with Java servlets and JSPs.

Java EE 5 uses Java annotations introduced by Java SE 5 to make development and configuration of Java EE 5 software easier for the developer. One such use of these annotations in Java EE is to use the @EJB injection in a servlet to access an Enterprise JavaBean (EJB). However, to use the @EJB annotation in a servlet, the servlet must be executed in a Java Servlet 2.5 environment.

Section 5.1 of the System Administrator's Guide to Deploying OpenLaszlo Applications covers how to implement "OpenLaszlo-enabled web applications" and Section 5.2 covers how to create a minimal OpenLaszlo server. In either case, it is likely that a developer would start with the web.xml provided with the OpenLaszlo distribution. There is no issue with this unless the developer wants to use Servlet 2.5 support for annotations.

To run Java 2.5 servlets with annotations (such as the @EJB) in conjunction with OpenLaszlo, the web.xml file that comes as part of the OpenLaszlo servlet distribution (as of 4.0.6) should be changed from being a Servlet 2.2 web.xml file to a Servlet 2.5 web.xml file. The web.xml file that is delivered with the OpenLaszlo distribution (as of 4.0.6) begins as shown (Servlet 2.2 web.xml format):

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"

For a Servet 2.5-compatible web application, the Servlet 2.5 web.xml file should be changed to start as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi=""

Note that the web.xml file for Servlet 2.2 is defined by a DTD while the web.xml file for Servlet 2.5 is defined by XML Schema. Also note that there should be no carriage return between the and the in the Servlet 2.5 web.xml, but I used one above to allow it to fit better on the page.

Changing the web.xml file that is delivered with OpenLaszlo as part of its servlet distribution from 2.2 to 2.5 allows servlets accessed from within the same WAR file to make use of annotations and the injections that come with annotations. An alternative approach would be to bundle EJB-accessing servlets or servlets needing annotations in a separate WAR from the WAR assembled from OpenLaszlo-delivered components.

No comments: