jetty

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

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

  server.setHandler(context);
 

  context.addServlet(new ServletHolder(new HelloServlet()), "/*");
		server.start();
		
	}
	
	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 {
			
			response.setContentType("text/html");
			response.setStatus(HttpServletResponse.SC_OK);
			response.getWriter().println(greeting);
		}
		
	}

}

URL:

http://myhost:8080/hello/

Output:

Hello World

  
This was an example of how to embed Jetty with Servlet in Java.

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.
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