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:
public static void main(String args) throws Exception
Server server = new Server();
SocketListener listener = new SocketListener();
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:
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"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure 1.2//EN" "http://jetty.mortbay.org/configure_1_2.dtd"> <Configure class="org.mortbay.jetty.Server"> <Call name="addListener"> <Arg> <New class="org.mortbay.http.SocketListener"> <Set name"port">8080</Set> </New> </Arg> </Call> <Call name="addWebApplication"> <Arg>/</Arg> <Arg>./webapps/template/</Arg> </Call> </Configure>
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.