BACK | INDEX | EXIT | NEXT

# The Basics

Introduction
What is Jetty

#

Jetty is both an HTTP Server (like apache ) and a Servlet Container (like tomcat) running as a single Java component. It can be used stand-alone to deploy your static content, servlets, JSPs and web applications. Alternatively, it can be embedded into your Java application to add HTTP and Servlet capabilities.
Lightweight or Standards Based
Jetty can be used at two different levels: as the core HttpServer and the complete Jetty Server. The former provides an HTTP server with the ability to serve static content and servlets, whilst the latter supports richer configuration capabilities and the deployment of standard web applications.
The advantages of the HttpServer is that it is lightweight and embeddable and is highly customizable.
If you need standard Servlet Container support for the development and deployment of web applications, then use the enhanced Server (referred to usually as the Jetty Server). This has the added benefit of better support for configuration via XML. Skip down to an example of a web app deployment here.
Introduction to the HttpServer

The org.mortbay.http.HttpServer class provides a core HTTP server that listens on specified ports and accepts and handles requests.

The server is configured by method calls on the Java API . This code example creates a simple server listening on port 8080 and serving static resources (files) from the location ./docroot:

`HttpServer server = new HttpServer();` `SocketListener listener = new SocketListener();` `listener.setPort(8080);` `server.addListener(listener);` `HttpContext context = new HttpContext();` `context.setContextPath("/");` `context.setResourceBase("./docroot/");` `context.addHandler(new ResourceHandler());` `server.addContext(context);` `server.start();` Code Example: Creating a trivial HTTP server

`
`
The server is made stand-alone by placing the above code in the body of a `main` method (the HttpServer class itself has a `main` that can be used as an example). To use the server as a component within an application, include the above lines at an appropriate location within the application code.

The HttpServer provides a flexible mechanism for extending the capabilities of the server called "HttpHandlers". The server selects an appropriate HttpHandler to generate a response to an incoming request. The release includes handlers for static content, authentication and a Servlet Container.

Serlvets are the standard method for generating dynamic content, however the server can also be extended by writing custom HttpHandlers if servlets are insufficient or too heavyweight for your application.

More detailed information on the HttpServer class, including setting up port listeners, contexts and handlers is to be found in the section HTTP Server.

Introduction to the Web Application Server

The org.mortbay.jetty.Server class extends org.mortbay.http.HttpServer with XML configuration capabilities and a J2EE compliant Servlet Container.

The following code example demonstrates the creation of a server, listening on port 8080 to deploy a web application located in the directory .`/webapps/myapp:`

`
`

`Server server = new Server(); SocketListener listener = new SocketListener(); listener.setPort(8080); server.addListener(listener); server.addWebApplication("/","./webapps/myapp/"); server.start();` Code Example: Creating a Web Application Server

As mentioned previously, the Jetty Server is able to be configured via XML as an alternative to cutting code. The same web application as coded above can be deployed by this XML configuration file:

    <?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/myapp/</Arg>
      </Call>

    </Configure>

XML Example: Configuring a Web Application Server

To run Jetty with this XML file, execute this command:

> `java -jar start.jar myserver.xml
> `

To read more about Jetty as a web application server, click here.
To skip to more information on the XML file format click here.

BACK | INDEX | EXIT | NEXT