Embedding Jetty

Jetty is a lightweight open source Java-based HTTP Server and Servlet container. Jetty’s small footprint makes it perfect for embedding into larger Java applications, in fact Jetty is used by the likes of Jboss Application Server and Apache Geronimo.

Embedding Jetty is extremely easy. For the most part you just create a new instance of a Jetty server, indicate where the server can find the resource files, such as HTML/PNG files, maybe add a Web Application Resource (WAR), and start the server. Here is the code step by step, of course the following code assumes you have added the required jars.

Server server = new Server(8080);

The above creates an instance of the Jetty server (org.mortbay.jetty.Server) listening at port 8080, but does not start the server. At this point we would need to add a default resource handler with the location of the directory which contains the public HTML, JPG, JS, and additional files which will can be requested by clients.

ResourceHandler publicDocs = new ResourceHandler();

I would also like to add support for a Java-based web application.

String webappPath = “c:/path/to/webapp.war”;
String contextPath = “/webapp”;
WebAppContext webapp = new WebAppContext(webappPath, contextPath);

Now I need to add the public resource and web application handlers to the Jetty server.

HandlerList hl = new HandlerList();
hl.setHandlers(new Handler[]{publicDocs, webapp});

At this point, you can start the server simply by calling the start method.


Once you have the above code up and running point your browser to http://localhost:8080 to connect to the Jetty server, of course you would need some index.html in the public docs directory. In this example, if where to connect to the web application described here you can do so by directing your browser to http://localhost:8080/wepapp.

Technorati Tags: , , , , , , , ,

5 Responses to “Embedding Jetty”

  • Vijay Says:

    Good simple explanation. It will be great to see some more examples of jetty usage. I have seen some with luntbuild launching jetty and tapestry 5 using jetty to launch selenium server.


  • Paul Says:

    Is it possible to have war on the java classpath and load from the classpath rather than a file location?

  • Charlie Mason Says:

    That was a great help, thanks for the post. I had been struggling to get Jetty Launcher to work. Run Jetty Run the new slimed down version of Jetty Launcher doesn’t seem to give you as many config options.

    I needed to enable Jetty plus so I could use JINI for database connection pooling. To initialise Jetty plus all you have to do is add the code below after you create the WebAppContext but before you start the server.

    webapp.setConfigurationClasses(new String[]{ “org.mortbay.jetty.webapp.WebInfConfiguration”, “org.mortbay.jetty.plus.webapp.EnvConfiguration”, “org.mortbay.jetty.plus.webapp.Configuration”, “org.mortbay.jetty.webapp.JettyWebXmlConfiguration”, “org.mortbay.jetty.webapp.TagLibConfiguration”});

    Also theres a typo in the last line of your code, theres an r missing from sever. Thanks again.

  • dan Says:

    This is a great little tutorial. I am just trying to use jetty for a small simple task and a lot of the other tutorials are little less to the point of my interests.

    Thank You!!!!

  • isometric Says:

    Dude, you are a godsend. I was beating my head against the wall for the last 24 hours trying to figure out how to serve my static files (css, js, etc) Resource Handler to the rescue!

Leave a Reply