Setting Contexts in Jetty

With this example we are going to demonstrate how to set the Contexts in a Jetty server. 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. In short, to set contexts in Jetty you should:

  • Create a handler to be set on the server. HelloHandler extends 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.
  • Create a new org.eclipse.jetty.server.Server in port 8080.
  • Create a new org.eclipse.jetty.server.handler.ContextHandler. A ContextHandler provides a common environment for multiple Handlers, such as: URI context path, class loader, static resource base.Typically a ContextHandler is used only when multiple contexts are likely.
  • Set the context path, the resource base and the classLoader using setContextPath(String contextPath), setResourceBase(String resourceBase) and setClassLoader(ClassLoader classLoader) API methods of org.eclipse.jetty.server.handler.ContextHandler.
  • Set the ContextHandler to the Server and the HelloHandler to ContextHandler and 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;

public class SettingContextsInJetty {
	public static void main(String[] args) throws Exception {
		Server server = new Server(8080);
		ContextHandler context = new ContextHandler();





  context.setHandler(new HelloHandler());
	public static class HelloHandler extends AbstractHandler {
		public void handle(String target, Request baseRequest,
				HttpServletRequest request, HttpServletResponse response)
				throws IOException, ServletException {
			response.getWriter().println("Hello World");





Hello World

This was an example of how to set the Contexts in a Jetty server 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