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
Simple Jetty HelloWorld Webapp

One thought on “Simple Jetty HelloWorld Webapp

  • May 26, 2017 at 4:06 pm

    Tks. Couldn’t have been simpler.


Leave a Reply

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