Embedding Jetty Web Server

Jetty is a seriously cool product. If you’re not familiar with it, Jetty is a fully functional Web Server written in 100% pure java. The list of supported features is very comprehensive (still growing in each new release) and the web server is capable of pretty much anything Tomcat can do, and then some.

What makes Jetty even better is that it requires no install and is platform independent. This makes it highly ideal for deploying rapid proof-of-concept applications to say a kiosk system: simply put Jetty in a directory on a USB, deploy your web-app (html, php, j2ee, ruby, etc) to the directory tree, and then simply copy the structure on any machine you want to run it on.

Additionally, since Jetty is written as a pure Java web server, you are free to import the main jar libraries into your own J2SE Java application to enable web server like functionality. You could use this for complex projects to make your own web server built on top of Jetty with highly customized features, or you could use it for small tools that could benefit from http functionality (such as enabling a http admin interface for a long-running process).

The beauty of it is that it’s so ridiculously simple to use! The following code is a fully functional example of a simple HTTP echo server (compiled with jetty-6.1.11.jar, jetty-util-6.1.11.jar and servlet-api-2.5-6.1.11.jar in classpath):

import java.io.IOException;
import java.io.InputStream;

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

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.handler.AbstractHandler;

public class HttpStub {
public static void main(String[] args) throws Exception {
if(args.length != 1) {
System.err.println("usage: java -jar HttpStub listen_port");

int port = Integer.parseInt(args[0]);

Server server = new Server();
Connector connector = new SocketConnector();
server.setConnectors(new Connector[] { connector });

Handler handler = new HelloHandler();


public static class HelloHandler extends AbstractHandler {
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest();

private static String streamToString(InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
return out.toString();

You may easily extend this into your own HTTP Proxy server with logging, redirection, anonymity, etc. Or turn it into a HTTP REST or Web Service stub with customized responses based on regex pattern matching in request text bodies or URI end-points, and so on.

And if that wasn't enough, Jetty is also a fully open-source Apache License 2.0 distribution!

Note: Code formatted with http://formatmysourcecode.blogspot.com/


  1. Hi is there a way to redirect my http request on port 80 to a https request on 443 using embedded jetty


Post a Comment

Popular posts from this blog

Wkhtmltopdf font and sizing issues

Import Google Contacts to Nokia PC Suite

Can't delete last blank page from Word