Java 18: Feature 1: JEP 408 : Simple Web Server

Introduction

Simple Web Server (SWS) is an interesting feature introduced in Java 18. It is a lightweight, minimal static web server. It provides basic server functionality and serves single directory with static files.

There are many alternatives exist already, hence, SWS is just the most simple web server. It is not a feature rich Web Server.

Simple Web Server helps in general development & debugging and in educational related activities.

SWS is a minimal HTTP server for serving a single directory hierarchy. It is based on the web server implementation in the com.sun.net.httpserver package. The package is officially supported, and it is extended with APIs to simplify server creation and enhance request handling.

SWS can be used via –

  • command-line tool jwebserver
  • programmatically via its API

Use Cases

There are variety of use cases where SWS can be used. Few important cases are mentioned below –

  • Creating an in-memory file server for unit testing purpose
  • Serving a zip file system for internal testing
  • Serving a Java RunTime directory for debugging
  • Combining a file handler with a predefined response handler for unit testing and automation suites
  • Web development internal testing
  • Web application/ service internal testing
  • Static content for checking/ testing purposes

Key Points

  1. SWS supports HTTP 1.1 only.
  2. It does not support HTTPS.
  3. Once started successfully, the server runs until it is stopped. On Mac/ Linux/ Unix platforms, the server can be stopped by sending it a SIGINT signal ( Ctrl+C in a terminal window).
  4. MIME types are configured automatically. Example – .html files are served as text/ html and .java files are served as text/ plain.
  5. Only idempotent HEAD and GET requests are served. Any other requests receive 501or a 405 response. GET requests are mapped to the directory being served, as follows: –
  • If the requested resource is a file, its content is served.
  • If the requested resource is a directory that contains an index file, the content of the index file is served.
  • Otherwise, the names of all files and subdirectories of the directory are listed. Symbolic links and hidden files are not listed or served.

SWS – Command Line Tool

Start SWS using command line by giving command as jwebserver –

% ./jwebserver

Binding to loopback by default. For all interfaces use “-b 0.0.0.0” or “-b ::”.

Serving /Users/esha/Tools/jdk-18.jdk/Contents/Home/bin and subdirectories on 127.0.0.1 port 8000

URL http://127.0.0.1:8000/

Default behaviour of SWS

SWS starts default at

  • Port – 8000
  • Host/ IP – localhost/ 127.0.0.1
  • Directory – Current directory contents will be served
  • Output Level – info. By default every request is logged to console.

Custom behaviour of SWS

  • Start SWS by giving custom port

% ./jwebserver -p 8888 

Binding to loopback by default. For all interfaces use “-b 0.0.0.0” or “-b ::”.

Serving /Users/esha/Tools/jdk-18.jdk/Contents/Home/bin and subdirectories on 127.0.0.1 port 8888

URL http://127.0.0.1:8888/

  • Start SWS by giving directory other than the current directory

% ./jwebserver -d /Users/esha

Binding to loopback by default. For all interfaces use “-b 0.0.0.0” or “-b ::”.

Serving /Users/esha and subdirectories on 127.0.0.1 port 8000

URL http://127.0.0.1:8000/

  • Start SWS – To bind server to all interfaces

% ./jwebserver -b :: or % ./jwebserver -b 0.0.0.0

  • Start SWS – Change output level

./jwebserver -o none/ info/ verbose

Help of jwebserver

% ./jwebserver -h         

Usage: jwebserver [-b bind address] [-p port] [-d directory]

                  [-o none|info|verbose] [-h to show options]

                  [-version to show version information]

Options:

-b, –bind-address    – Address to bind to. Default: 127.0.0.1 (loopback).

                        For all interfaces use “-b 0.0.0.0” or “-b ::”.

-d, –directory       – Directory to serve. Default: current directory.

-o, –output          – Output format. none|info|verbose. Default: info.

-p, –port            – Port to listen on. Default: 8000.

-h, -?, –help        – Prints this help message and exits.

-version, –version   – Prints version information and exits.

SWS – API

API approach is also provided for using simple web server. This will provide a concise and intuitive programmatic solution for creation and customization of sever components i.e. server, handler and filter.

For SWS API new classes are provided. These new classes are available in com.sun.net.httpserver package and built on existing classes and interfaces – HttpServer, HttpHandler, Filter, and HttpExchange. New classes for SWS are mentioned below –

  • SimpleFileServer
  • HttpHandlers
  • Request

HTTP Request support

  • HTTP 1.1 is supported
  • Only idempotent request types i.e. HEAD and GET methods are supported

Demonstration

Below code is to inspect the class files in the run-time image of a remote system, mainly for debugging/ diagnostic purpose.

package com.thinkconstructive;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.file.FileSystems;
import com.sun.net.httpserver.SimpleFileServer;

import static com.sun.net.httpserver.SimpleFileServer.OutputLevel;

public class SWS_JRT {
    private static final InetSocketAddress LOOPBACK_ADDR =
            new InetSocketAddress(InetAddress.getLoopbackAddress(), 8888);

    public static void main( String[] args ) {
        var fileSystem = FileSystems.getFileSystem(URI.create("jrt:/"));
        var absolutePath = fileSystem.getPath("modules").toAbsolutePath();
        var httpServer = SimpleFileServer.createFileServer(LOOPBACK_ADDR, absolutePath, OutputLevel.INFO);
        httpServer.start();
    }
}

Execution – Give below command using any browser –

  • Listing complete content of Java Runtime Environment – > http://127.0.0.1:8888/
  • Accessing a particular class – > http://127.0.0.1:8888/java.naming/com/sun/naming/internal/FactoryEnumeration.class

Get the Code for above demo from here – GITHUB

SWS DOES NOT SUPPORT

  • HTTPS and HTTP/2 are not supported
  • POST and PUT requests are not supported
  • Any type of security such as OAuth, HTTPS, etc. are not supported

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *