2

In the example listed on the undertow documentation site, it shows how to configure 2 servlets and their mappings. But I can't find how to configure the default Servlet to allow directory listing and resource serving.

DeploymentInfo servletBuilder = Servlets.deployment()
        .setClassLoader(ServletServer.class.getClassLoader())
        .setContextPath("/myapp")
        .setDeploymentName("test.war")
        .addServlets(
                Servlets.servlet("MessageServlet", MessageServlet.class)
                        .addInitParam("message", "Hello World")
                        .addMapping("/*"),
                Servlets.servlet("MyServlet", MessageServlet.class)
                        .addInitParam("message", "MyServlet")
                        .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(8080, "localhost")
        .setHandler(path)
        .build();
server.start();

It's an embedded undertow server in a runnable jar.

David Hofmann
  • 5,683
  • 12
  • 50
  • 78
Carlos Laspina
  • 2,013
  • 4
  • 27
  • 44

1 Answers1

3

When adding servlets, also add the "default" servlet in the list of servlets with

Servlets.servlet("default", DefaultServlet.class)
.addInitParam(DefaultServlet.DIRECTORY_LISTING, "true")
.addInitParam(DefaultServlet.DEFAULT_ALLOWED, "true")
.addInitParam(DefaultServlet.ALLOW_POST, "false")
.addInitParam(DefaultServlet.RESOLVE_AGAINST_CONTEXT_ROOT, "true")

Then before deploying the container add this to the DeploymentInfo

servletBuilder.setResourceManager(new ClassPathResourceManager(App.class.getClassLoader(), "webapp"));

Then anything you put inside the webapp folder that is packaged in the jar will work exactly the same as any normal war/webapp folder deployed in any app server.

You can use any name or folder, webapp was just an example.

David Hofmann
  • 5,683
  • 12
  • 50
  • 78