Jetty Tutorial Overview      Back Index Exit Next

Jetty is both a HTTP Server (like apache) and a Servlet Container (like tomcat) running as a single Java component. Jetty can be used as a stand-alone servlet/container with which you can deploy and server your static content, servlets, JSPs or web applications. Jetty can also be used as a software component so that you may add HTTP and/or Servlet capabilities to your java product or application.

HTTP Server
The org.mortbay.http.HttpServer class provides core HTTP server, which is responsible for listening on ports and accepting and handling requests. The base package does not know about Servlets but provides and extensible HttpHandler architecture and handlers for serving static resources.

HTTP Server instances are configured via the Java API. A simple file serving server can be configured as follows:

    HttpServer server = new HttpServer();
    server.addListener(new InetAddrPort(8080));
    HandlerContext context = server.addContext("/");
    context.setResourceBase("./docroot/");
    context.setServingResources(true);
    server.start();
This configuration listens on port 8080 and serves static content from the ./docroot directory.

Jetty Server
The org.mortbay.jetty.Server class extends HttpServer with XML configuration capabilities and a J2EE compliant Servlet container. The Server can be configured via its Java API:

    Server server = new Server();
    server.addListener(new InetAddrPort(8080));
    server.addWebApplication("/","./webapps/myapp/","./etc/webdefaults.xml");
    server.start();
This configuration listens on port 8080 for requests and servers content from the web application in ./webapps/myapp.

The Server class may also be configured via XML and the code above is equivalent to:

myserver.xml:
    <?xml version="1.0"  encoding="ISO-8859-1"?> 
    <!DOCTYPE Configure PUBLIC 
     "-//Mort Bay Consulting//DTD Configure 1.0//EN" 
     "http://jetty.mortbay.org/configure_1_0.dtd">

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

      <Call name="addListener">
        <Arg><New class="org.mortbay.util.InetAddrPort>
          <Arg>8080</Arg>
        </New></Arg>
      </Call>

      <Call name="addWebApplication">
        <Arg>/</Arg>
        <Arg>./webapps/myapp/</Arg>
        <Arg>./etc/webdefault.xml/</Arg>
      </Call>

    </Configure>
This XML file can be run with the following command:
    java org.mortbay.jetty.Server myserver.xml

Servlet Container
The Servlet container implements the 2.2 Servlet Specification. This allows servlets to be added to the container in several ways:

XML Configuration
As can be seen above, the XML configuration mechanism of Jetty is equivalent to making calls on the Java API. For clarity, the remainder of this document will mostly give examples as API calls rather than the more verbose XML. The XML format will also be discussed in detail and examples given in the XML Configuration tutorial.

Back Index Exit Next