Simple Web Server in JDK 18

JDK 18 is shipped with Simple Web Server for ad hoc testing, prototyping, and also you can learn performance testing by running it local. In this blog post, we are going to see how you can use Simple Web Server in JDK 18.

Prerequisites

The only prerequisite is to install OpenJDK 18 on your system. You can install it by downloading the binaries from here, https://openjdk.java.net/projects/jdk/18/.

After setting the environment variable, you can verify the installation using java -version command.

JDK 18
JDK 18

By default, JDK 18 shipped with Simple Web Server. Go to the path where you extracted and mapped the environment variable. e.g. on Windows, I have placed the JDK in C:\java\jdk-18.

Go to JAVA_HOME\bin, there will be an executable called jwebserver.exe.

Launching Simple Web Server

Launching Simple Web Server is easy and straight forward. Open your favorite terminal, and enter jwebserver.

By default, it will launch the web server at http://127.0.0.1:8000 by default.

What is a Simple Web Server?

Simple Web Server is a minimal HTTP web server which serves the current directory hierarchy. It is based on the com.sun.net.httpserver implementation. The package has been improved and is now available via command line for developers and testers.

Verify the Simple Web Server

To verify the Simple Web Server, just enter jwebserver -version which will print the current version.

Simple Web Server Commands

To get help, enter jwebserver -h

jwebserver -h
jwebserver -h

To bind the server to all the interfaces, enter jwebserver -b 0.0.0.0

To allocate a port, enter jwebserver -p 5000

To change the log output, enter jwebserver -o none|info|verbose

To change the directory, enter jwebserver -d <absolute-path

Simple Web Server in JDK 18
Simple Web Server in JDK 18

Limitations

The following are the limitations of the Simple Web Server:

  • supports only GET and HEAD methods.
  • supports HTTP 1.1. No HTTP 2.
  • supports only HTTP. No HTTPS.

Features

By default, jwebserver serves the current directory in a hierarchy.

directory listing
directory listing

If the directory being served has index.html, then the content will be rendered.

index.html
index.html

MIME type are automatically configured. *.html files will be served as text/html, *.java files will be served as text file. Other files will be downloaded, if you click on the file name.

MIME Type
HTML MIME Type
java MIME type
java MIME type

The Simple Web Server keeps running till the session is terminated. To terminate, press CTRL + C.

Extending the Simple Web Server

Suppose, if you want to extend and customize the Simple Web Server, you can leverage com.sun.net.httpserver package and the new classes SimpleFileServerHttpHandlers, and Request.

Other Options

The other alternative is to use java -m jdk.httpserver. jwebserver uses java -m jdk.httpserver under the hood. You can also use the below command to launch the Simple Web Server.

java -m jdk.httpserver -b 0.0.0.0 -p 5000

Final Words

The Simple Web Server is a minimal HTTP web server which will be helpful for quick and ad hoc testing, prototyping, and for learning performance testing concepts. It will be helpful for developers and QA professionals. But if you prefer something more like a demo app, you can spin up containers; there are a lot of open source apps are available which have more features.

Simple Web Server in JDK 18

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join the fastest growing Performance Engineers club at ClubhouseSHOW ME HOW
+ +
Share via
Copy link