Embedded Jetty: How to upgrade from version 6 to 9

February 6, 2014

Some say you might never change a running system. In case of our servlet container Jetty this might have been right, since we haven’t updated it for more than five years. There was no need to. It worked fine. Nevertheless the change had to come someday and this is how we managed to update our 5+ year old running servlet container without any trouble.

JettyLogo

Beautiful tools never get old.

At SoftwareDEMO, we use open source HTTP server Jetty as an embedded servlet container. Jetty is purely written in Java and is (as of version 7 in 2009) part of the Eclipse Foundation. It’s mostly used for M2M communications and less frequently to serve web pages and appliations to end users.

Some days ago, I made some improvements in SWD Agent, our application to easily connect any machine to our cloud platform. There I stumbled over lots of ClosedChannelExceptions in the debugging output. I wondered why we haven’t seen them before… We use Logback with an email appender for every error message. With this many exceptions in my logfile, it should have been flooding my inbox. But a closer look revealed that the exception had just a DEBUG level, not ERROR, not even WARN. I guess, the exception is not critical, but it struck my developer’s honor. My logfiles are supposed to be clean!

5+ years, still stable

So I searched the internet and finally found an old bug ticket in the Codehaus Jira, where Jetty was hosted before it moved to Eclipse. The ticket was marked Closed and is pretty old so I was about to leave. Fortunately, I looked again and saw the affected version and compared it with the Jetty version we currently have in SoftwareDEMO. We still have a five year old server in our component. Although Jetty 6 is still considered as stable, I wanted to update to the newest version. But a lot has changed in 5 years so it’s a little more than just swapping a library.

Here’s what I had to do to update our embedded Jetty which is running two servlets, a RESTful service and uses SSL.

Getting Jetty 9

Head over to the Jetty download page to get the latest stable version or use a maven repository.

Updating your dependency libraries

The following libraries were needed in Jetty 6:

Libraries for embedded Jetty 6

In Jetty 9, the libraries are split into smaller libraries:

Libraries embedded Jetty 9

If you don’t care about smaller assets and want to make sure you don’t miss a thing, you can also use the aggregated Jetty library „jetty-all“ that you can download from Maven repositories.

Aggregated Jetty 9 library

Updating your source code

Five years pass its mark – even on a software project. Although the changes in Jetty are relatively small, some things altered. To save you the time for searching StackOverflow, here’s a quickstart.

Your code for starting Jetty 6 with two servlets, a RESTful service and SSL might look like this (WARNING! This code is highly simplified! Don’t just copy and paste – think!):

For Jetty 9 it looks a little different, but you get the same result with this code:

Resources

For more information and even more examples for embedding Jetty, head over to The Definitive Reference, e.g.

This page: Print Send by email Share on: