Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use the Servlet API with Undertow?

I'm just discovering how Undertow works and I was surprised by its api:

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();

Is there a way to use the more convenient servlet api like this somehow?

Undertow server = Undertow.builder()
                .addHttpListener(8080, "localhost")
                .setHandler(new HttpHandler() {
                    @Override
                    public void handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
                        // ...
                    }
                }).build();
        server.start();

What I'm trying to achieve is to replace the currently working Jetty container which uses the servlet api by Undertow but after reading the docs and the source I can't seem to find a way to do so. I'm not using .war files just an embedded Jetty. Do someone has any pointers?

like image 654
Adam Arold Avatar asked Mar 13 '23 08:03

Adam Arold


1 Answers

It's documented in section Creating a Servlet Deployment. Here's a MCVE based on the documentation provided that you've the dependencies right, along with the Servlet API.

package com.stackoverflow.q35269763;

import io.undertow.Handlers;
import io.undertow.Undertow;
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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test {

    public static void main(String... args) throws Exception {
        DeploymentInfo servletBuilder = Servlets.deployment().setClassLoader(Test.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(8888, "localhost").setHandler(path).build();
        server.start();
    }

}

When you open http://localhost:8888/myapp/myservlet in your favourite webbrowser after copy'n'paste'n'running the above code, you'll see

Hello World!

like image 153
BalusC Avatar answered Mar 25 '23 00:03

BalusC