Undertow web server

Undertow

Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO.

Undertow has a composition based architecture that allows you to build a web server by combining small single purpose handlers. The gives you the flexibility to choose between a full Java EE servlet 4.0 container, or a low level non-blocking handler, to anything in between.

Undertow is designed to be fully embeddable, with easy to use fluent builder APIs. Undertow’s lifecycle is completely controlled by the embedding application.

Why Undertow

  • HTTP/2 Support
    • Undertow supports HTTP/2 out of the box, no overriding the boot class path required.
  • HTTP Upgrade Support
    • Support for HTTP upgrade, to allow multiple protocols to be multiplexed over the HTTP port.
  • Web Socket Support
    • Undertow provides full support for Web Sockets, including JSR-356 support.
  • Servlet 4.0
    • Undertow provides support for Servlet 4.0, including support for embedded servlet. It is also possible to mix both Servlets and native undertow non-blocking handlers in the same deployment.
  • Embeddable
    • Undertow can be embedded in an application or run standalone with just a few lines of code.
  • Flexible
    • An Undertow server is configured by chaining handlers together. It is possible to add as much or as little functionality as you need, so you don’t pay for what you are not using.
  • Lightweight
    • Undertow is extremely lightweight, with the Undertow core jar coming in at under 1Mb. It is lightweight at runtime too, with a simple embedded server using less than 4Mb of heap space.

Show me the code

Here is a simple Hello World server using Async IO:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class HelloWorldServer {
public static void main(final String[] args) {
Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Hello World");
}
}).build();
server.start();
}
}

Maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>2.0.20.Final</version>
</dependency>

<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>2.0.20.Final</version>
</dependency>

<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-websockets-jsr</artifactId>
<version>2.0.20.Final</version>
</dependency>