jetty

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

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();

  connector.setPort(8080);

  server.addConnector(connector);

  server.setStopAtShutdown(true);

		Handler handler = new AbstractHandler() {

			@Override
			public void handle(String target, Request request,
					HttpServletRequest servletRequest,
					HttpServletResponse servletResponse) throws IOException,
					ServletException {
				servletResponse.setContentType("text/html");
				servletResponse.setStatus(HttpServletResponse.SC_OK);
				servletResponse.getWriter().println("Hello");
				request.setHandled(true);
			}
		};
		
		server.setHandler(handler);


  server.start();
		
	}

}

URL:

http://myhost:8080/

Output:

Hello

  
This was an example of how to launch Jetty in embedded mode 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