Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

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 http://download.eclipse.org/jetty/ 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.

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. About this website

7. Jetty resources

7.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

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

See Licence.