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">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<groupId>org.eclipse.jetty.demo</groupId>
<artifactId>jetty-helloworld-webapp</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.5.v20170502</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
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 { @Override 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
3 Comments
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:
UTF-8
1.8
1.8
Marcio Coutinho · 03/06/2021 at 14:24
‘
UTF-8
1.8
1.8
‘
Comments are closed.