In order to deploy your web application on Undertow, you need to use Maven to manage the dependency through the web application project.
First we create a new Java project named UndertowDemo on Eclipse:

Then create a class named App.java under the project:

Open the App.java:

Here we modify the main method to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class App {
public static void main(final String[] args) {
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();
}
}

Now we got a lot of errors:

Right click the project, click Configure -> Conver to Maven Project:

There will be a window of creating POM, leave all the input as default and click Finish:

Then Eclipse will open the new created pom.xml:

Now modify pom.xml to add dependencies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>1.0.1.Final</version>
</dependency>
</dependencies>

And change plugin to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>App</mainClass>
</configuration>
</plugin>

Now we have the pom.xml like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>UndertowDemo</groupId>
<artifactId>UndertowDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>1.0.1.Final</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

Then go back to App.java and you will find that you can import Undertow:

After importing all the dependencies, all the errors in App.java have been removed:

1
2
3
4
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;


Then copy the project folder to you remote AWS instance:

1
2
3
4
// On Mac
scp -r -i path/of/security/key/filename.pem path/of/the/project/UndertowDemo username@instanceDNS
// On Windows
pscp -r -i path/of/security/key/filename.ppk path/of/the/project/UndertowDemo username@instanceDNS


Login to the instance, run mvn -v to check if Maven is available:

If Maven is not installed on the instance, then download the Maven zip file to your local machine:

And copy it to your remote AWS instance:

1
2
3
4
// On Mac
scp -i path/of/security/key/filename.pem path/of/the/mavenzip/filename username@instanceDNS
// On Windows
pscp -i path/of/security/key/filename.ppk path/of/the/mavenzip/filename username@instanceDNS


Go back to your instance, unzip the Maven zip file, if unzip is not available then install it by:

1
sudo yum install unzip

Now unzip the Maven zip file and add the path of Maven bin to the variable of PATH:

1
export PATH=/home/ec2-user/hunter/apache-maven-3.3.9/bin:$PATH

Now you can use Maven on the instance:

Go to the project folder, meaning you are under the same directory as the pom.xml file:

Compile the project:

1
mvn compile


You can see BUILD SUCCESS, then run the project:

1
mvn exec:java


Now launch the browser and open the DNS of the instance as a URL, and you get:

Try the DNS with 8080:

OK, go back to the instance and stop the service, open the App.java:

The potential reason of this might be the value of host which we use localhost here, now change it to the DNS of this instance:

Save and quit, and GO BACK to the directory where we place POM.xml, then compile and run the project again.
Alright, launch the browser and open the DNS of the instance as a URL, and you get:

Try the DNS with 8080:

That means we do not open port 80 for this web application, stop the service.
Like what we did in this article:

Redirect connections on port 80 to 8080

run:

1
2
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
sudo iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080

Then run the project again. Now launch the browser and open the DNS of the instance as a URL, and you get: