Embedding Jetty with Netbeans 7 (and Maven)

I’m on a project right now where I need to embed Jetty 7, there are some good documentation and blog posts out there on how to do this as well as numerous examples provided here. Today I’ve taken some of the samples and modified them a little. I’ll take two servlets and serve them through Jetty 7 (embedded) using Netbeans 7 and Maven.

Using Netbeans 7 create a new Maven Java Application and call it EmbeddedJettyDemo. Right-click on the dependencies and select add dependency. In the query textbox use the text jetty-server and add in version 7.4.5 or later. Add another dependency and use jetty-servlet and add in the same version: 7.4.5 or later (make sure the version matches with the jetty-server you just added.

Note: If Jetty is not showing up for you then you might need to add additional Maven repositories.

Create two new Java class files in the default package called: HelloServlet.java and HelloServlet2.java

The code for the App.java class is as follows (this is the class we will run):

This is very basic and you can see it adds two servlets. The first servlet is mapped as: http://localhost:8080/demo/* and as http://localhost:8080/demo/1 The second servlet is mapped as: http://localhost:8080/demo/2