Java 18 Introduces Simple Web Server
Typically, if you want to serve even a simple HTML file, you would need to start an HTTP Server. Serving an HTML file implies running it on a web server as a web page, which is different from opening an HTML file in a browser. An HTTP Server interprets URLs (web addresses) using the HTTP (Hypertext Transfer Protocol) to serve web pages. Several lightweight server frameworks such as Jetty and Netty are available in addition to some production-grade servers such as Apache Tomcat, and Apache httpd.
The commercial grade servers such those mentioned are not built-in, and need to be downloaded and installed separately. Sometimes all you need is to serve some static files.
Some other languages such as PHP, and Ruby include a built-in HTTP Server. Java 18 adds a minimal, out-of-the-box HTTP Server.
Not Your Commercial Grade Server
The built-in server included with Java 18 is not a commercial-grade HTTP server and lacks advanced features such as authentication, access control, and encryption. But the benefit is that no configuration and setup is needed. The Simple HTTP Server only serves static files.
How Simple Is It?
The Simple HTTP Server is quite “simple” to use. All you need to do is start the HTTP Server with a single command. First, verify that the Java version is 18 with the java -version command.
Next, start the Simple HTTP Server with the jwebserver command. As the command output indicates, the HTTP Server is ready to serve static files from the directory from which the command is issued and sub-directories. The output from the common includes a message similar to : Serving C:\Users\dvohra and subdirectories on 127.0.0.1 port 8000 URL http://127.0.0.1:8000/. The directory C:\Users\dvohra is the directory from which the command is issued.
First Static File
Create an HTML file index.html in the root directory from which the HTTP Server is started, C:\Users\dvohra in the example, and copy content from index.html on GitHub to it.
Serve the index.html file at URL http://127.0.0.1:8000/. The output in the browser is shown:
Does it remind you of what PHP and Ruby provide?
What Could You Use the Simple HTTP Server for?
The Simple HTTP Server may be used for:
- Ad Hoc coding
- Browsing files and directories
- Web Service testing
Definitely not to serve CGI and servlets, and full-featured applications.
Some Command-Line Options Are Supported
Some command-line options are supported. You could, for example, change the server’s bind address, the port the server binds to, the directory to serve, and the output format. All the supported options may be listed with jwebserver -h command.
Java 18 is not a Long Term Support version, and whether the Simple HTTP Server makes it to the next LTS depends on user feedback and demonstrated usefulness.