Jetty, embedded servlet container

Originally published December 03, 2008.
Information provided in this post may be out of date.

There’s no law which states web servers and servlet containers must be large, separate pieces of software.  If there were, Jetty would be a dangerous outlaw (possibly even public enemy number one).  Jetty is an open-source embeddable web server and servlet container, written in Java.  It’s small, fast, and easy to embed – perfect for self-contained applications.

Let’s use an example to discover just how easy embedding Jetty can be.  Say we have two servlets, one should be mapped to /service/one and the other /service/two, served on port 8080.

Server server = new Server();
Connector connector = new SelectChannelConnector();

ContextHandlerCollection contexts = new ContextHandlerCollection();

Context context = new Context(contexts, "/service");
context.addServlet(ServiceOne.class, "/one");
context.addServlet(ServiceTwo.class, "/two");


That’s it; easy.  For information and more examples on embedding Jetty in your Java application, read Embedding Jetty.