Jetty Logo
Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery

Chapter 18. Embedding

Table of Contents

Jetty Embedded HelloWorld
Embedding Jetty

Jetty Embedded HelloWorld

This section provides a tutorial that shows how you can quickly develop embedded code against the Jetty API.

Downloading the Jars

Jetty is decomposed into many jars and dependencies to achieve a minimal footprint by selecting the minimal set of jars. Typically it is best to use something like Maven to manage jars, however this tutorial uses an aggregate Jar that contains all of the Jetty classes in one Jar. You can manually download the aggregate jetty-all Jar and the servlet api Jar using wget or similar command (for example, curl ) or a browser. Use wget as follows:

mkdir Demo
cd Demo
wget -O jetty-all.jar -U none http://repo1.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/9.0.0-RC1/jetty-all-9.0.0-RC1.jar
wget -O servlet-api.jar -U none http://repo1.maven.org/maven2/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar

Writing a HelloWorld Example

The Embedding Jetty section contains many examples of writing against the Jetty API. This tutorial uses a simple HelloWorld handler with a main method to run the server. You can either download or create in an editor the file HelloWorld.java with the following content:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloWorld extends AbstractHandler {

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        server.setHandler(new HelloWorld());
        server.start();
        server.join();
    }
}

Compiling the HelloWord example

The following command compiles the HelloWorld class:

javac -cp servlet-api.jar:jetty-all.jar HelloWorld.java

Running the Handler and Server

The following command runs the HelloWorld example:

java -cp .:servlet-api.jar:jetty-all.jar HelloWorld

You can now point your browser at http://localhost:8080 to see your hello world page.

Next Steps

To learn more about Jetty, take these next steps:

  • Follow the examples in Embedding Jetty to better understand the jetty APIs.

  • Explore the complete jetty javadoc

  • Consider using Jetty and Maven (see Using Maven) to manage your Jars and dependencies.

See an error or something missing? Contribute to this documentation at Github!