Running a server with the Oracle Java ME Embedded Client

Introduction

This article describes how to install the Jetty server and connect to servlets using a browser across the network. If you have not already, read the following articles that describe how to get Linux running on a BeagleBoard and how to install Oracle Java Micro Edition Embedded Client.

Ensure that you complete the network setup in the first article.


Get Jetty and Set Up

Jetty is a lightweight web server and servlet container. Jetty is designed to run on the Java Platform, Standard Edition (Java SE). Because the CDC stack of the Oracle Java Micro Edition Embedded Client differs from Java SE, you cannot get the latest version of Jetty and expect it to run.

Because the CDC stack does not include XML parsing APIs, Jetty is configured and runs differently than it would with Java SE.

The appropriate version of Jetty for a CDC stack is 6.1.26, available here:

Get jetty-6.1.26.zip. Unzip it on your development machine.

To run Jetty on Java SE, you already have everything you need. However, for a CDC stack, you run Jetty in “embedded” mode, meaning that you write a small main class that starts the Jetty server.

Jetty includes some examples that show it running in embedded mode. The examples are located in examples/embedded. You must build the examples before you can use them, and you need Maven to build the examples. On an Ubuntu development machine, install Maven with the following command:

alix@livadia:~$ sudo apt-get install maven2

Go to the examples directory and build them:

alix@livadia:~$ cd jetty-6.1.26/examples/embedded
alix@livadia:~/jetty-6.1.26/examples/embedded$ mvn
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------
[INFO] Building Embedded examples
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------
.
.
.
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Tue Feb 08 08:47:08 EST 2011
[INFO] Final Memory: 21M/51M
[INFO] ------------------------------------------------------------
alix@livadia:~/jetty-6.1.26/examples/embedded$

Maven downloads various packages and performs the build, which takes approximately 4 minutes. Subsequent builds are much faster.

You have everything you need to run servlet code on the Beagleboard.


Install Jetty on the BeagleBoard

1. Put the Jetty files in an archive:

alix@livadia:~/jetty-6.1.26/examples/embedded$ cd ../../..
alix@livadia:~$ tar zcvf jetty-transfer.tar.gz jetty-6.1.26

2. Copy the archive you just created to the BeagleBoard:

alix@livadia:~$ scp jetty-transfer.tar.gz \
ubuntu@10.42.43.11:/home/ubuntu
ubuntu@10.42.43.11's password: jetty-transfer.tar.gz 100% 21MB 5.3MB/s 00:04 alix@livadia:~$

3. Unpack the archive on the BeagleBoard:

ubuntu@omap:~$ tar zxvf jetty-transfer.tar.gz

Run a Servlet

In the BeagleBoard console, define some variables:

ubuntu@omap:~$ export CVM=\
/home/ubuntu/Oracle_JavaME_Embedded_Client/binaries/production/bin/cvm
ubuntu@omap:~$ export JETTY=/home/ubuntu/jetty-6.1.26 ubuntu@omap:~$

Run one of the Jetty embedded examples:

ubuntu@omap:~$ $CVM -cp $JETTY/lib/servlet-api-2.5-20081211.jar:$JETTY/lib/
jetty-util-6.1.26.jar:$JETTY/lib/jetty-6.1.26.jar:$JETTY/examples/embedded/targe
t/classes org.mortbay.jetty.example.OneServletContext
2011-02-08 08:06:51.492:INFO::Logging to STDERR via org.mortbay.log.StdErrLog 2011-02-08 08:06:51.714:INFO::jetty-6.1.26 2011-02-08 08:06:52.195:INFO::Started SocketConnector@0.0.0.0:8080

The example looks complicated, but it is not. You are running cvm, passing it Jetty libraries and the embedded examples in the class path, and running one of the examples, OneServletContext.

Jetty is now running on the Beagleboard.

Switch to another machine on the same network. In a browser, ender the IP address of the Beagleboard and port 8080. For example, if your BeagleBoard is 10.0.1.6 on your network, the URL is http://10.0.1.6:8080/. The browser shows the output:

See the Jetty output in a browser


Write Your Own Code

Use the Jetty embedded examples as a starting point for your own code. For example, review the OneServletContext example. It is straightforward. It has a main() method that starts the Jetty server on port 8080 and adds a servlet, HelloServlet, mapped to any requests.

HelloServlet is a standard servlet subclass with a simple doGet() implementation.

To get started writing your own servlet code, you can make a copy of this example and start making changes to it. You can still use mvn to build—it will find your new source file and build it correctly.


Summary

This article describes the basic steps for running a servlet container on the BeagleBoard. The Oracle Java Micro Edition Embedded Client and the Jetty server provide a powerful base for building sophisticated, networked embedded applications.

false ,,,,,,,,,,,,,,,,