Embedding Jetty with Servlet

This is an example of how to embed Jetty server with Servlet. 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. Here we will see how to deploy a servlet into jetty. The simple servlet is deployed and mounted on a context so as to be able to process requests. Embedding Jetty with Servlet implies that you should:

  • Create a HelloServlet that extends javax.servlet.http.HttpServlet and overrides its doGet(HttpServletRequest request, HttpServletResponse response) method, to set the response status and the content-type.
  • Create a Server Object that will listen on port 8080.
  • Create a ServletContextHandler that is backed by an instance of a Servlet and register it with the Server object.
  • Register the HelloServlet to the Server and mount it on a given context path.
  • Start the server.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.enterprise;


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

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class EmbeddingJettyWithServlet {
	public static void main(String[] args) throws Exception {
		Server server = new Server(8080);
		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);



  context.addServlet(new ServletHolder(new HelloServlet()), "/*");
	public static class HelloServlet extends HttpServlet {
		private static final long serialVersionUID = -6154475799000019575L;
		private static final String greeting = "Hello World";

		protected void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {





Hello World

This was an example of how to embed Jetty with Servlet 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


Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron 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