In this tutorial we cover a brand new Java 18 feature, an HTTP server embedded in the JDK. This tutorial is about understanding and implementing web user profiles using Java 18’s SimpleFileServer class.
What is SimpleFileServer?
According to the official documentation, this HTTP file server and its components are intended for testing, development, and debugging purposes only. This isn’t a full fledged production-ready server, but rather a tool intended to smooth out the experience of new (and old) Java developers that need to quickly have a web server up-and-running for development purposes. It is comprised of three main components;
- an HttpServer that can be called at the base or given address,
- an HttpHandler that serves files from a given directory path that the developer specified, and
- an optional Filter that prints log messages related to the exchanges handled by the server.
Here is a few practical concepts on how to use SimpleFileServer to show static web pages, in this case user profiles.
Requirements to Use SimpleFileServer
The following is required to use the SimpleFileServer:
- Java Development Kit (JDK 18) or above;
- Basic Java Knowledge
JDK 18 can be downloaded from the JDK Download Page. After downloading the JDK, perform the installation by following the wizard guide.
Creating our Application
This tutorial was done with IntelliJ IDEA Ultimate. But the general process is the same with any Java development platform.
- Open your IDE and create a Java project with a class named SimpleBrowser inside the src directory, include the main method as shown below:
public class SimpleBrowser {
public static void main(String[] args) {
}
}
- Create another directory inside the project folder with the name Pages. The project directory should have the following structure:
- Add static content to the Pages folder. To effectively use the Pages folder, simply add static contents such as profile.html, style.css, style.js, and carbrands.js as shown in the image below:
- Use SimpleFileServer.createFileServer method. The createFileServer static method returns an HttpServer. The response comes from a specified file server path as shown in the code snippet below:
import com.sun.net.httpserver.SimpleFileServer;
import java.io.File;
import java.net.InetSocketAddress;
import java.nio.file.Path;
public class SimpleBrowser {
public static void main(String[] args) {
// parameters
Integer port = 8080;
File myFile = new File("Pages");
String path = myFile.getAbsolutePath();
System.out.println(path);
SimpleFileServer.OutputLevel outputLevel = SimpleFileServer.OutputLevel.VERBOSE;
// create the server
var server = SimpleFileServer.createFileServer(
new InetSocketAddress(port),
Path.of(path),
outputLevel));
// start the server
server.start();
}
}
In this code, we explicitly assign a port number and the directory of the static files to be served. Line 16 is where the createFileServer method is called to setup the HttpServer. Line 22 is used to start the server.
After starting the server, the files are served at the specified port on localhost which can be accessed from a web browser. An example is shown below:
The code used in this tutorial is available here.