Home Tutorials Training Consulting Products Books Company Donate Contact us









Online training

Events

Quick links

Share

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 https://www.eclipse.org/jetty/download.html 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

http://localhost:8080/
jetty10

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
httpClient.start();

// do a get request
ContentResponse response = httpClient
.GET("http://www.vogella.com")
.get();

// Issue a post request
ContentResponse response = httpClient
.POST("http://www.vogella.com/entity/1")
.param("p", "value")
.send()
.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 java.io.IOException;
import java.io.PrintWriter;

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,
                                                      ServletException
    {
        response.setContentType("text/html; charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);

        PrintWriter out = response.getWriter();

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

        baseRequest.setHandled(true);
    }
}
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 {

    @Execute
    public void execute()  throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Server server = new Server(8080);
                try {
                    server.getConnectors()[0].getConnectionFactory(HttpConnectionFactory.class);
                    server.setHandler(new HelloHttpRequestHandler());

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


    }
}

6. Jetty resources

7. vogella training and consulting support

Copyright © 2012-2018 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.