Using the default Servlet in WildFly applications

This article will teach you how to use the default Servlet available in Undertow Web Server for some concerns, such as directory listing or static resources serving.

Most Web servers have a default Servlet available. What’s the use case for a default Servlet? You can use it mainly for two reasons:

  • To serve static resources or list files under a folder
  • To restrict the access to some static resources matching one or more extensions
  • To allow or forbid directory listing

Let’s see how to configure the DefaultServlet.

Default Servlet Configuration

Undertow ships with the Class io.undertow.servlet.handlers.DefaultServlet which is the default Servlet responsible for serving up resources. It works both as Handler and a Servlet. If no filters match the current path then the resources will be served up asynchronously using the HttpHandler.handleRequest(io.undertow.server.HttpServerExchange) method.

Otherwise the request is handled as a normal Servlet request.

Here is a sample usage:

<web-app id="myapp" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>io.undertow.servlet.handlers.DefaultServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>

</web-app>

The above example will allow serving static resources under the /static folder of your Web application. Here is a sample Web application which uses this structure:

src
└── main
    ├── java
    │   └── org
    │       └── jboss
    │           └── as
    │               └── quickstarts
    │                   └── helloworld
    │                       ├── HelloService.java
    │                       └── HelloWorldServlet.java
    └── webapp
        ├── index.html
        ├── static
        │   └── picture.png
        └── WEB-INF
            ├── beans.xml
            └── web.xml

Configuration parameters for the Default Servlet

You can customize the behaviour of the Default Servlet using the following init-parameters:

  • directory-listing: you can enable directory listing for your application
  • allowed-extensions: you can choose which extensions are allowed. By default the following ones are allowed: “js”, “css”, “png”, “jpg”, “gif”, “html”, “htm”, “txt”, “pdf”, “jpeg”, “xml”.
  • disallowed-extensions: you can choose to deny one or more extensions
  • resolve-against-context-root: if true allows resolving the resource against the Web context root
  • allow-post: if true allows sending POST requests to the Servlet. Default is false

And here is a sample configuration which includes extra parameters to allow XML extensions, directory listing and resolve against the Root Web cotext:

<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>io.undertow.servlet.handlers.DefaultServlet</servlet-class>
        <init-param>
            <param-name>allowed-extensions</param-name>
            <param-value>xml</param-value>
        </init-param>
        <init-param>
            <param-name>directory-listing</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>resolve-against-context-root</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>
</web-app>
Found the article helpful? if so please follow us on Socials