Last lesson, we showed how to create a XOE package containing only static content in the form of a simple XHTML file.
Now we will show how to use Java and the XOE servlet facility to build a Hello World application that generates the content dynamically.
Starting with the example from last lesson, modify the package-in.xml file to look like:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE package PUBLIC "-//TVT//DTD XOEPKG 1.0//EN" "http://www.xoe.org/DTD/xoe-package.dtd"> <package name="helloworld-servlet" version="1.0" task='true'> <info> <name>Hello World Servlet example</name> <description>A simple package demonstrating servlets.</description> <maintainer> <name>Jim Pick</name> <email>jim@transvirtual.com</email> </maintainer> <copyright> <year>2001</year> <holder>Transvirtual Technologies Inc.</holder> </copyright> <licence type="open" common="GPL" /> </info> <requires> <dep name='xhtml' predepends='true' ns='http://www.xoe.org/installer/base/package'/> <dep name='xoe-servlet' predepends='true' ns='http://www.xoe.org/installer/base/package'/> </requires> </package>
The changes were:
renaming of the package to helloworld-servlet
addition of a dependency on the xoe-servlet package
Let's create a directory to hold the Java code. We follow the standard Java naming scheme, and put it in it's own directory, eg. org/tvt/examples/HelloWorld.java:
helloworld-servlet/ |-- org | `-- tvt | `-- examples | `-- HelloWorld.java |-- package-in.xml `-- resources `-- index.xhtml
Here's the code:
/** * HelloWorld servlet example * Copyright 2001, Transvirtual Technologies, Inc. */ package org.tvt.examples; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*; import org.xoe.servlet.XoeServlet; import org.xoe.servlet.XoeServletRequest; import org.xoe.servlet.XoeServletResponse; import org.xoe.core.dom.XDocument; public class HelloWorld extends XoeServlet { protected void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType ("text/html"); XDocument doc = getRelativeDocument ("index.xhtml"); res.getWriter ().print (doc); } }
In order for the servlet to work, it needs to be registered as a XOE service when the package is installed. In order to do this, we need a services.xml file in the resources directory. It looks like this:
<?xml version="1.0" encoding="UTF-8"?> <!-- $tvt: servletapp.xml,v 1.2 2001/12/18 00:22:15 alex Exp $ --> <services> <service class='org.tvt.examples.HelloWorld' config='helloworld.xoe-config'/> </services>
At install time, the service will start, and the HelloWorld class will be instantiated. The config file for the service is specified as helloworld.xoe-config, which should also be placed in the resources directory. Here's what it looks like:
<?xml version="1.0"?> <xoe-config xmlns="http://xoe.org/ns/2001" name="Hello World" icon="icon.gif" class="org.tvt.examples.HelloWorld" />
This file supplies various bits of information to the XoeServlet service. Most important is the "class" attribute, which specifies which Java class has the code implementing the service. The "name" and "icon" attributes are used to display a descriptive name and icon for the service. The icon.gif file should also be placed in the resources directory.
In order for the service to appear as an icon in the Program Manager, a special resource file called shortcuts.xml must be created. It simply maps the shortcut to the appropriate service config file.
<?xml version="1.0" encoding="UTF-8"?> <shortcuts> <lnk file="helloworld.xoe-config" /> </shortcuts>
The final set of files should look like:
helloworld-servlet/ |-- org | `-- tvt | `-- examples | `-- HelloWorld.java |-- package-in.xml `-- resources |-- helloworld.xoe-config |-- icon.gif |-- index.xhtml |-- services.xml `-- shortcuts.xml
Let's build the package now:
$ ~/tvt/build/cvs-head-work/xoe/tools/xoe-buildpackage xoe-buildpackage: building package in: . xoe-buildpackage: package name: helloworld-servlet_1.0.xoe xoe-buildpackage: maintainer: Jim Pick <jim@transvirtual.com> xoe-buildpackage: checking XML well-formedness xoe-buildpackage: validating package-in.xml xoe-buildpackage: calculating build deps using: /home/jim/.xoe/bootstrap-build.xml depends on: xml-renderer-core depends on: xhtml depends on: xhtml-services depends on: xoe-interfaces depends on: xoe-servlet xoe-buildpackage: compiling classes xoe-buildpackage: installing xoe-buildpackage: zipping xoe-buildpackage: converting package-in.xml --> package.xml xoe-buildpackage: re-zipping xoe-buildpackage: saving package to: /home/jim/.xoe/packages/helloworld-servlet_1.0.xoe
Now, let's try it out!
First, use the package manager to search for helloworld-servlet, and install it.
Now, you should see an icon for it in the Program Manager:
Click on the "Hello World" icon, and the application is launched: