Launch Jetty in embedded mode

In this example we shall show you how to launch Jetty server in embedded mode. The Jetty Web Server provides an HTTP server and Servlet container capable of serving static and dynamic content either from a standalone or embedded instantiations. Jetty has a rich history of being embedded into a wide variety of applications. To launch it in embedded mode one should perform the following steps:

  • Create a Server Object that will listen to port 8080. In order to do so create a org.eclipse.jetty.server.nio.SelectChannelConnector and add to it port 8080, and then add the connector to the server.
  • Create a handler to be set on the server, org.eclipse.jetty.server.handler.AbstractHandler, that is the Jetty component that deals with received requests. In its handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) method it needs the target of the request, which is either a URI or a name from a named dispatcher, the Jetty mutable request object, which is always unwrapped, the immutable request object, which might have been wrapped and the response, which might have been wrapped. The method sets the response status, the content-type and marks the request as handled before it generates the body of the response using a writer.
  • Set the Handler to the Server and start the server,

as described in the code snippet below.  

package com.javacodegeeks.snippets.enterprise;


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

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;

public class LaunchJettyInEmbeddedMode {
	public static void main(String[] args) throws Exception {
		Server server = new Server();

  SelectChannelConnector connector = new SelectChannelConnector();




		Handler handler = new AbstractHandler() {

			public void handle(String target, Request request,
					HttpServletRequest servletRequest,
					HttpServletResponse servletResponse) throws IOException,
					ServletException {







This was an example of how to launch Jetty in embedded mode in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button