jetty

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 java.io.IOException;

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.setContextPath("/hello");

  context.setResourceBase(".");

  context.setClassLoader(Thread.currentThread().getContextClassLoader());

  server.setHandler(context);
 

  context.setHandler(new HelloHandler());
		server.start();
		
	}
	
	public static class HelloHandler extends AbstractHandler {
		
		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);
			baseRequest.setHandled(true);
			response.getWriter().println("Hello World");
			
		}
		
	}

}

URL:

http://myhost:8080/hello/

Output:

Hello World

 
This was an example of how to set the Contexts in a Jetty server in Java.

Ilias Tsagklis

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

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button