July 8, 2005

Jetty and the future of browser-based applications

Author: Costa Walcott

Despite its name, the Web browser is good for more than just browsing the Web. More and more projects use the browser as a secondary or, in many cases, primary means of communicating with the program. The main drawback of this approach is that it requires the user to configure and maintain his own Web server, or else requires the program to come bundled with a Web server. Jetty, a small open source Web server written entirely in Java, is simple for both programmers and users, making it a good choice for bundled Web server.

Jetty, which is widely used with Java and open source projects, can act as a standalone program or as an API. It can serve HTML and Web applications written using JSPs or servlets. The Jetty library is small in size, and the server requires few system resources.

To see how you can serve content using very few lines of code, download Jetty's tar.gz file, uncompress it using the commands tar -zxvf jetty-4.2.24.tar.gz, then cd jetty-4.2.22. After that the following is all you need to start serving content:

import org.mortbay.http.SocketListener;
import org.mortbay.jetty.Server;

class testserver
 public static void main(String[] args) throws Exception
   Server server = new Server();
   SocketListener listener = new SocketListener();
   server.addWebApplication("/", "./webapps/template/");

Place this code in testserver.java in the Jetty directory. There are a few jar files that Jetty needs, so you should add them to your classpath:

CLASSPATH=lib/org.mortbay.jetty.jar:lib/javax.serv let.jar:ext/jasper-runtime.jar:ext/jasper-compiler.jar:ext/xercesImpl.jar

Now you can compile and run this test server:

javac -classpath $CLASSPATH testserver.java
java -classpath $CLASSPATH:. testserver

Pointing your browser to http://localhost:8080 should connect you to the Jetty server.

Looking at the source code can show us a bit of how Jetty works. The Server class accepts requests and sends them to various contexts that provide the conent. Listener classes such as SocketListener wait for HTTP connections. The code includes listeners for services like SSL as well. Finally, by adding a Web application, we create a context to which requests may go. Here we are telling connections to root ("/") to be routed to the template Web application provided by Jetty.

Another useful feature in Jetty is that almost everything available programatically can be written using XML documents as well. The example above can be replicated in the following XML code:

<?xml version="1.0" encoding="ISO-8859-1"?>
 "-//Mort Bay Consulting//DTD Configure 1.2//EN"

<Configure class="org.mortbay.jetty.Server">

  <Call name="addListener">
      <New class="org.mortbay.http.SocketListener">
        <Set name"port">8080</Set>

  <Call name="addWebApplication">


Try saving this to testserver.xml, then start Jetty by running java -jar start.jar testserver.xml, and you should see the same content being served. A pre-existing application could easily add a Web interface to display data and foster user interaction using this method.

You can use Jetty for more than just Web applications. The current programming model for desktop applications usually requires backend code, which does the main work for the program, and GUI code, which gathers and displays information to the user. Often the development of the GUI portion of the application can be more difficult and time-consuming than anything else.

Another worry is making GUIs portable. Even with tools like Java, which are inherently cross-platform, developers still may worry about the "look and feel" of each platform on which their program may run. Having an application interface through the Web browser is especially convenient for users to be able to log into, say, Samba or MySQL by just typing the address into their browser, especially when the alternative is logging into the machine and typing in commands manually.

In addition to adding Web interfaces to network services running on remote hosts, companies are adding Web access to programs running locally on client machines, either as a supplement to a traditional GUI or a replacement for one. This model is appealing because of users' widespread use of and comfort with Web browsers, and because of the inherent cross-platform nature of Web content, which differs from, say, a Java GUI. In addition, from a developer's standpoint, creating an HTML GUI is easy and quick.

In general, requiring a user to maintain his own Web server is not a viable approach. The alternative is to embed a Web server within the code of a program with tools like Jetty. As such tools grow in popularity, we can expect to see more and more applications whose primary means of communicating with the user is through the browser.

Costa Walcott is the President of Draconis Software, whose network monitoring solution uses a Jetty-powered Web interface.

Click Here!