Java 18 new features - Simple Web Server

Updated:

Java 18 introduces a new feature, a Simple Web Server (JEP 408).

Why a new server when the Java world has already top-class web servers (Tomcat, Jetty etc.)?

The Simple Web Server aims to provide a lightweight and easy-to-use solution for small-scale tasks and educational purposes, making it an excellent tool for developers who need a quick way to serve static files or test their web applications without the overhead of a full-fledged server.

The details of the tutorial is in my video:

Example

This code refers to the video tutorial.

For our example we place all the files in the same folder.

folder structure

To test the Simple Web Server we provide an optional html file and a json file.

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>marmo.dev example</title> 
</head> 
<body> 
    hello java 18 
</body> 
</html> 

The json file:

{ 
  "name":"marco", 
  "job":"java developer" 
} 

We can start the Web Server with the command jwebserver from the command line inside our folder.

The default web server will serve the static pages using the port 8000, example with http://localhost:8000/test.json.

If you want to change the port you can use the parameter -p [port number]. You can find the list of the available options in the official documentation of jwebserver

the server shows the json data

Instantiate the Simple Web Server from a Java class

If you want to build your own custom simple web server, or you want to start a server from an application you can use the class SimpleFileServer:

import java.net.InetSocketAddress; 
import java.nio.file.Path; 
 
import com.sun.net.httpserver.SimpleFileServer; 
import com.sun.net.httpserver.SimpleFileServer.OutputLevel; 
 
public class SuperServer {  
  public static void main(String[] args) { 
    // parameters 
    Integer port = 8080; 
    String pathToServe = "/"; 
    OutputLevel outputLevel = OutputLevel.VERBOSE; 
 
    // create the server 
    var server = SimpleFileServer.createFileServer( 
      new InetSocketAddress(port), 
      Path.of(pathToServe), 
      outputLevel); 
 
      // start the server 
      server.start(); 
  } 
} 

In our example we can start the server from the code after having defined the port and the root folder of the static files.

For education and testing

presentation of the features

The goal is to provide an educational server that can start from the command line, without external dependencies.

The server has no ambitions, but it could be useful for trainings and, important for us, to quickly generate some REST responses during the development of our frontend application.

It’s very common to create a small node.js server to return a simple JSON to quickly see results in our UI during the frontend development. I created a post for the node.js implementation, you can find the link at the bottom of the page.

The new Java Simple Web server allows us to simulate a web service with just a JSON file and the command jwebserver, without the need to create a simple web server from scratch.

The implementation is very limited, it handles only GET requests. It’s possible to extend the features for our test purposes using the SimpleFileServer class.

Any future?

I think this feature could be useful for few use cases in our daily routine. Unfortunately sometimes it seems Java is simply trying to copy with poor implementations features present in other languages.

The JEP mentions that

Python, Ruby, PHP, Erlang, and many other platforms offer out-of-the-box servers run from the command line.
This variety of existing alternatives demonstrates a recognized need for this type of tool.

In my opinion we will have a result similar to jshell, the features is interesting with a nice initial potential, but it is added to Java with only the goal to 'copy' other languages without enough energy to really 'sell' it.

The nice new feature will be considered a toy by some developers and ignored by the majority.

You can find some examples and more details in the video integrated in this post.


You could be interested in

Angular: Local proxy server to improve frontend development

Mix local and remote REST answers to speed up frontend dev
2019-11-08
WebApp built by Marco using SpringBoot 3.2.4 and Java 21, in a Server in Switzerland