Linux.com

Feature

Jetty and the future of browser-based applications

By Costa Walcott on July 08, 2005 (8:00:00 AM)

Share    Print    Comments   

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();
   listener.setPort(8080);
   server.addListener(listener);
   server.addWebApplication("/", "./webapps/template/");
   server.start();
  }
}

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.ser<nobr>v<wbr></nobr> let.jar:ext/jasper-runtime.jar:ext/jasper-compile<nobr>r<wbr></nobr> <nobr> <wbr></nobr>.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"?>
<!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.

Share    Print    Comments   

Comments

on Jetty and the future of browser-based applications

Note: Comments are owned by the poster. We are not responsible for their content.

Jetty is great!

Posted by: ammoQ on July 08, 2005 05:41 PM
If you need nothing more than a small, easy-to-install, easy-to-use servlet container, Jetty does the job very well.

#

Jetty, Tomcat

Posted by: Anonymous Coward on July 08, 2005 08:42 PM
I do not understand why the Jetty is small and Tomcat is not small. I have been more involved with tomcat, but I don't see so much difference.

DG

#

Re:Jetty, Tomcat

Posted by: Anonymous Coward on July 08, 2005 11:04 PM
I think the main difference is packaging. A default install of Tomcat is huge by comparison to Jetty, and trimming down Tomcat seems to be much more work.

If all you need are simple web apps (JSPs, Servlets, or some other web framework), Jetty is as good as they come.

#

Jetty smaller than Tomcat, are you sure?

Posted by: Anonymous Coward on July 08, 2005 11:34 PM
I just checked the download size for each:
Tomcat 5.0.27 = 10,057 kB
Jetty 4.2.19 = 11,892 kB

(From a gentoo 'emerge -s' command)

#

Re:Jetty smaller than Tomcat, are you sure?

Posted by: Anonymous Coward on July 09, 2005 03:09 AM
Well, that's jetty-all. Jetty without the "Extras" directory (various additional integration libs... mostly unnecessary) is about 5.5MB.

Also, there's an "ext" dir that contains some default extensions weighing in at about 5 MB. On recent VMs, most of those can be thrown out, dropping the size by about another 4.5 MB.

In the end, it's super-easy to drop the whole thing down to less than 1MB.

Oh, and 4.2.19 is ancient. 5.1.4 is current.

#

Re:Jetty smaller than Tomcat, are you sure?

Posted by: Anonymous Coward on July 15, 2005 07:05 AM
I managed to strip down jetty to a 283KB jar file. I needed a simple servlet container for my application (see the Portable GUI posting) so I removed all other functionality than basic HTTP and servlet features.

#

Portable GUI

Posted by: Anonymous Coward on July 15, 2005 05:09 AM
Here is an example of an application that uses jetty to serve its GUI:
<a href="http://wiki-in-a-jar.sourceforge.net/" title="sourceforge.net">http://wiki-in-a-jar.sourceforge.net/</a sourceforge.net>

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya