With the jetty-maven-plugin and Servlet Annotations, it has never been simpler to start developing with Jetty! While we have not quiet achieved the terseness of some convention over configuration environments/frameworks/languages, it is getting close and only 2 files are needed to run a web application!

Maven pom.xml

A minimal maven pom.xml is need to declare a dependency on the Servlet API and use the jetty-maven-plugin.   A test project and pom.xml can be created with:

$ mkdir demo-webapp
$ cd demo-webapp
$ gedit pom.xml

The pom.xml file is still a little verbose and the minimal file needs to be at least:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

Annotated HelloWorld Servlet

Maven conventions for Servlet development are satisfied by creating the Servlet code in following source directory:

$ mkdir -p src/main/java/com/example
$ gedit src/main/java/com/example/HelloWorldServlet.java

Annotations allows for a very simple Servlet file that is mostly comprised of imports:

package com.example;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/*"}, loadOnStartup = 1)
public class HelloWorldServlet extends HttpServlet
 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws IOException
 response.getOutputStream().print("Hello World");

Running the Web Application

All that is left to do is to run the web application:

$ mvn jetty:run

You can then point your browser at http://localhost:8080/ to see your web application!

Next Steps

OK, not the most exciting web application, but it is a start.  From here you could:

  • Clone this demo from github.
  • Add more Servlets or some Filters
  • Add static content in the src/main/webapp directory
  • Create a web deployment descriptor in src/main/webapp/WEB-INF/web.xml
  • Build a war file with mvn install


someone · 26/05/2017 at 16:06

Tks. Couldn’t have been simpler.

Marcio Coutinho · 03/06/2021 at 14:23

Great! It still works in 2021, but I needed to include this setting:

    Marcio Coutinho · 03/06/2021 at 14:24


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *