Undertow

All

Intro

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

Undertow

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++.

Documentation

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

Tutorial

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 “0.0.0.0”
*/
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())
.setDeploymentName(“myapp”).setContextPath(“/myapp”)
.addServlets(Servlets.servlet(“myservlet”,
new HttpServlet() {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write(“Hello World!”);
}
}.getClass()).addMapping(“/myservlet”));

DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
manager.deploy();
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);
server.start();
}

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

resp.setContentType(“text/plain”);
resp.setCharacterEncoding(“UTF-8”);
resp.setContentLength(responseSize);

PrintWriter pw = resp.getWriter();
for (int i = 0; i < responseSize; i++) {
pw.append(‘X’);
}
}*/

}

REFs

 

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