New in Java 18: Simple Web Server jwebserver

In late March of this year, Java has been updated to 18. Next, I will pick some interesting content and introduce and learn from it in the form of articles and videos.

If you want to get content updates as soon as possible, those who are interested in the article form can follow my blog or public account (Program Ape DD), and those interested in the video form can follow

Simple web server: jwebserver

JEP 408: Simple Web Server  is a relatively independent new feature point introduced by Java 18 this time. We can start a mini web server that provides access to static resources through a command line tool.

The official positioning document of the tool says a lot:

Here I briefly summarize a few points for your convenience:

  • The purpose of construction is to apply to testing and teaching, not to replace advanced servers such as Jetty and Nginx
  • No security features such as authentication, access control, or encryption are provided
  • Only supports HTTP/1.1, not HTTPS
  • Only GET, HEAD requests are supported
  • Can be started from the command line, Java classes

Let's try the function of jwebserver through an example of building an HTML page.

HTML page preparation

Step 1: Create a folder, such as jwebserver

Step 2: Create an html file, just call  index.html it

Step 3: Just write some HTML content, such as the following (if you are lazy, you can directly get all the content needed in this case according to the prompts in the picture):

jwebserver command to start

Open a terminal and enter the command:

$ jwebserver

The startup is very fast, and you can see the following output:

Try to visit  http://127.0.0.1:8000/ , you can get the HTML content prepared before.

At the same time, I also saw the request log in the terminal:

127.0.0.1 - - [20/4月/2022:00:10:58 +0800] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [20/4月/2022:00:10:58 +0800] "GET /banner-spring-boot.png HTTP/1.1" 200 -

There are also some optional parameters about jwebserver, which are as follows:

-h or -? or --help
 Prints the help message and exits.

-b addr or --bind-address addr
 Specifies the address to bind to.  Default: 127.0.0.1 or ::1 (loopback).  For
 all interfaces use -b 0.0.0.0 or -b ::.

-d dir or --directory dir
 Specifies the directory to serve.  Default: current directory.

-o level or --output level
 Specifies the output format.  none | info | verbose.  Default: info.

-p port or --port port
 Specifies the port to listen on.  Default: 8000.

-version or --version
 Prints the version information and exits.

Among the more useful are

-b
-p
-d
-o

So a more complete startup command is as follows:

jwebserver -p 9000 -d / -b 127.0.0.1 -o info

This command is a  / server started from the root directory, the service port is 9000, the binding address is 127.0.0.1, and the console output level uses info.

Try to visit again:  http://127.0.0.1:9000/, you can see the folders and files under the root directory `/`:

how about it? Is it quite simple and easy to use? Well, today's sharing is here! If you encounter difficulties in the learning process? You can join our high-quality technical exchange group, participate in exchanges and discussions, and learn and progress better!

Also, don't walk away, follow me! Next update how to use Java code to start this server!

Guess you like

Origin blog.csdn.net/Trouvailless/article/details/124297064