Jetty Logo
Version: 9.4.29.v20200521
Contact the core Jetty developers at

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 for sponsored feature development

Chapter 21. Embedding

Table of Contents

Jetty Embedded HelloWorld
Embedding Jetty
Embedded Examples

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 required Jetty classes in one Jar. You can manually download the aggregate jetty-all.jar using curl or a browser.


The central Maven repository has started to aggressively reject/deny access to the repository from the wget command line tool (due to abusive use of the tool by some groups). The administrators of the central maven repository have stated that the recommended command line download tool is now curl.


The jetty-all jar referenced in this section is for example purposes only and should not be used outside of this context. Please consider using Maven to manage your project dependencies.

Use curl as follows:

> mkdir Demo
> cd Demo
> curl -o jetty-all-uber.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 with the following content:

//  ========================================================================
//  Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//      The Eclipse Public License is available at
//      The Apache License v2.0 is available at
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================

package org.eclipse.jetty.embedded;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloWorld extends AbstractHandler
    public void handle(String target,
                       Request baseRequest,
                       HttpServletRequest request,
                       HttpServletResponse response) throws IOException,
        // Declare response encoding and types
        response.setContentType("text/html; charset=utf-8");

        // Declare response status code

        // Write back response
        response.getWriter().println("<h1>Hello World</h1>");

        // Inform jetty that this request has now been handled

    public static void main(String[] args) throws Exception
        int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
        Server server = new Server(port);
        server.setHandler(new HelloWorld());


Compiling the HelloWord example

The following command compiles the HelloWorld class:

> mkdir classes
> javac -d classes -cp jetty-all-uber.jar

Running the Handler and Server

The following command runs the HelloWorld example:

> java -cp classes:jetty-all-uber.jar org.eclipse.jetty.embedded.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:

See an error or something missing? Contribute to this documentation at Github!(Generated: 2020-05-21)