Undertow is a lightweight server that is a subsystem of JBoss.


The source code is here: https://github.com/undertow-io/ and it’s quite interesting to see java code again, after a long time with python/c++.


Undertow documentation is pretty neat and can be found here. It states that the core part of Undertow is the lightweight async handlers.


There are many tutorials on the internet but the ones I recommended are: this and this, especially Baeldung tutorial.

Deploy simple servlet

package com.baeldung.undertow;

import io.undertow.Undertow;
import io.undertow.Handlers;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;

import java.io.IOException;

import io.undertow.server.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.undertow.util.Headers;

public class SimpleServer {

static void simpleHandler(HttpServerExchange exchange)
exchange.getResponseHeaders().add(Headers.CONTENT_TYPE, “text/plain”);
exchange.getResponseSender().send(“Adding Handler!”);

public static void main(String[] args) throws Exception
int port = 8080;
* “localhost” will ONLY listen on local host.
* If you want the server reachable from the outside you need to set “”
String host = “localhost”;

* This web server has a single handler with no routing.
* ALL urls are handled by the helloWorldHandler.
//Undertow server = Undertow.builder()
// Add the helloWorldHandler as a method reference.
// .addHttpListener(port, host, SimpleServer::simpleHandler)
// .build();

//Deploy Get
DeploymentInfo servletBuilder = Servlets.deployment().setClassLoader(SimpleServer.class.getClassLoader())
new HttpServlet() {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write(“Hello World!”);

DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
PathHandler path = Handlers.path(Handlers.redirect(“/myapp”)).addPrefixPath(“/myapp”, manager.start());

Undertow server = Undertow.builder().addHttpListener(port, host).setHandler(path).build();

//logger.debug(“starting on http://” + host + “:” + port);

/* Version of setBufferSize – TomCat*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {


PrintWriter pw = resp.getWriter();
for (int i = 0; i < responseSize; i++) {




One thought on “Undertow

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s