Mike Christianson
Mike Christianson

Categories

Tags

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();
connector.setPort(8080);
server.addConnector(connector);

ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);

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

server.start();
server.join();

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