I expect that we'll see this more commonly as more Android-based hardware devices come out other than mobile phones. In this particular case, the Jetty server is intended to drive a dynamic Web-based management tool for an application running on an Android device. I need to get a proper Web app project set up that pre-compiles the JSPs before they get to the Android device but this post is intended to show what libraries are required and how to set up Jetty to run on the Android device. I'm using a regular Android emulator out of Eclipse and a G1 running Cyanogen (API 8) for testing at this point.
General Stuff
I had to use
Jetty version 7.3.0.v20110203 to avoid XML validation feature that causes troubles on Android 2.2 devices.
I merged JARs from the Jetty download into a single one to add to my app that I called
jetty.jar
. For example, here's a little shell script I used for my WAR Deployed Web App.
jar xf ../jetty-continuation-*.jar
jar xf ../jetty-http-*.jar
jar xf ../jetty-io-*.jar
jar xf ../jetty-security-*.jar
jar xf ../jetty-server-*.jar
jar xf ../jetty-servlet-*.jar
jar xf ../jetty-util-*.jar
jar xf ../jetty-webapp-*.jar
jar xf ../jetty-xml-*.jar
jar xf ../servlet-api-2.5.jar
jar cf ~/Documents/ECLIPSE_PROJECT_PATH/libs/jetty.jar *
I see that a work-around for the IPv4 / IPv6 issue is still required. I used
// work-around for Android defect 9431
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("java.net.preferIPv6Addresses", "false");
Since the emulator uses slirp for the guest networking, you have to set up port forwarding from the host. For the G1, I used the real address of the phone (shows up in the status bar). So for the emulator I'd point my browser at
http://localhost:8080/
but from my G1 I'd point at
http://10.1.1.8:8080/
(of couse, this IP is local to my LAN).
adb -e forward tcp:8080 tcp:8080
Simple Web App
Just to make sure that I could start a server and have it listening as expected, I deployed a simple static app.
This is the list of JARs that I had to put into my app.
- jetty-continuation-7.3.0.v20110203.jar
- jetty-http-7.3.0.v20110203.jar
- jetty-io-7.3.0.v20110203.jar
- jetty-server-7.3.0.v20110203.jar
- jetty-util-7.3.0.v20110203.jar
- servlet-api-2.5.jar
Here is the Jetty server clip I put in my app
onCreate
webServer = new Server(8080);
Handler handler = new AbstractHandler() {
public void handle(String target, Request request, HttpServletRequest servletRequest,
HttpServletResponse servletResponse) throws IOException, ServletException {
servletResponse.setContentType("text/html");
servletResponse.setStatus(HttpServletResponse.SC_OK);
servletResponse.getWriter().println("<h1>Hello World</h1>");
((Request) request).setHandled(true);
}
};
webServer.setHandler(handler);
try {
webServer.start();
Log.d(LOG_TAG, "started Web server @ " + getPublicInetAddress());
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification( R.drawable.web_server_icon, "WebServer", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT; // Notification.FLAG_NO_CLEAR;
Intent notificationIntent = new Intent(this, MyApp.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "Web Server" , "Web Server Listening @ " + getPublicInetAddress(), contentIntent);
notificationManager.notify(NOTIFICATION_ID_WEB_SERVER, notification);
}
catch (Exception e) {
Log.d(LOG_TAG, "unexpected exception starting Web server: " + e);
}
WAR Deployed Web App
The next step was to deploy an external servlet via a WAR file. This turned out to be a lot more difficult than I imagined because of checkin r2770 in Jetty-7. It turns out that an XmlParser validation feature was added to Jetty that Android 2.2 doesn't support. Once I moved to a version of Jetty older than that revision I was able to move forward.
This is the list of JARs that I had to put into my app.
- jetty-continuation-7.3.0.v20110203.jar
- jetty-http-7.3.0.v20110203.jar
- jetty-io-7.3.0.v20110203.jar
- jetty-security-7.3.0.v20110203.jar
- jetty-server-7.3.0.v20110203.jar
- jetty-servlet-7.3.0.v20110203.jar
- jetty-util-7.3.0.v20110203.jar
- jetty-webapp-7.3.0.v20110203.jar
- jetty-xml-7.3.0.v20110203.jar
- servlet-api-2.5.jar
The code:
webServer = new Server(8080);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setTempDirectory(new File("/sdcard/my-work/"));
webapp.setWar("/sdcard/my-webapps/my-ROOT.war");
webServer.setHandler(webapp);
try {
webServer.start();
Log.d(LOG_TAG, "started Web server @ " + getPublicInetAddress());
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification( R.drawable.web_server_icon, "WebServer", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT; // Notification.FLAG_NO_CLEAR;
Intent notificationIntent = new Intent(this, MyApp.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "Web Server" , "Web Server Listening @ " + getPublicInetAddress(), contentIntent);
notificationManager.notify(NOTIFICATION_ID_WEB_SERVER, notification);
}
catch (Exception e) {
Log.d(LOG_TAG, "unexpected exception starting Web server: " + e);
}
The
my-work
directory gets created fine. I need to push the WAR file out: for the emulator I used
adb push ../my-ROOT.war sdcard/my-webapps
Next Steps
- I need to submit a patch to Jetty to get a
try / catch
around the XmlParser validation feature that is causing trouble so we can use more recent versions.
- I need to set up JSP pre-compiling in my Web project so I can add JSPs to make developing the UI easier.
- I need to dig into the Android device and figure out how to set the low-port listen capability. Right now, I have to listen on the 1024+ port range so I use the common port 80. For a real device, I need to change this to port 80 but that requires a Linux capability change - probably in device boot parameters.
Here's the change that I think needs to be dis-armed a bit.
$ svn diff -c 2770 XmlParser.java
Index: XmlParser.java
===================================================================
--- XmlParser.java (revision 2769)
+++ XmlParser.java (revision 2770)
@@ -107,6 +107,7 @@
_parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", validating);
_parser.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", true);
_parser.getXMLReader().setFeature("http://xml.org/sax/features/namespace-prefixes", false);
+ _parser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", validating);
}
catch (Exception e)
{