This tutorial describes the Eclipse Jetty Server.

1. What is Jetty?

Jetty is a web container. It has the following core components.

  • Http Server

  • Servlet Container

  • HTTP Client

The current version of Jetty is Jetty 9 which supports the Servlet API 3.1.

2. Installation

Download Jetty from and extract the zip file somewhere to your filesystem.

This update site contains also a Eclipse p2 update site, in case you want to use Jetty in an OSGi environment.

3. Start Jetty from the JAR file

To start Jetty, switch on the command line to the installation directory and issue the following command.

java -jar start.jar

This starts Jetty on


To stop Jetty press Ctrl+C.

To start Jetty as Windows service you can use Apache Procrun.

4. Using the HttpClient

The following code show a few examples of using the HttpClient of Jetty.

// Get a Client
HttpClient httpClient = new HttpClient();

// Start server

// do a get request
ContentResponse response = httpClient

// Issue a post request
ContentResponse response = httpClient
.param("p", "value")
.get(5, TimeUnit.SECONDS);

5. Embedding Jetty in an Eclipse RCP application

Use Jetty in an Eclipse RCP application, add all Jetty plug-ins and the javax.servlet plug-in as dependency to the MANIFEST.MF and the product.

package com.vogella.eclipse.embeddedjetty.handlers;

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.handler.AbstractHandler;

public class HelloHttpRequestHandler extends AbstractHandler
    final String greeting;
    final String body;

    public HelloHttpRequestHandler()
        this("Hello World");

    public HelloHttpRequestHandler( String greeting )
        this(greeting, null);

    public HelloHttpRequestHandler( String greeting, String body )
        this.greeting = greeting;
        this.body = body;

    public void handle( String target,
                        Request baseRequest,
                        HttpServletRequest request,
                        HttpServletResponse response ) throws IOException,
        response.setContentType("text/html; charset=utf-8");

        PrintWriter out = response.getWriter();

        out.println("<h1>" + greeting + "</h1>");
        if (body != null)

package com.vogella.eclipse.embeddedjetty.handlers;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
public class StartJettyHandler {

    public void execute()  throws Exception{
        Runnable runnable = new Runnable() {
            public void run() {
                Server server = new Server(8080);
                try {
                    server.setHandler(new HelloHttpRequestHandler());

                } catch (Exception e) {
        new Thread(runnable).start();


6. Jetty resources

7. vogella training and consulting support

Copyright © 2012-2019 vogella GmbH. Free use of the software examples is granted under the terms of the Eclipse Public License 2.0. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.