In late March this year, the Java version has been updated to 18. Next, DD plans to continue to do a series, mainly to update various updates starting from Java 9, but I will not introduce all of them. I will mainly pick some interesting content and introduce and learn it in the form of articles and videos. In order to get the content updates in the first time, those interested in the article form can follow my blog or public account (Program Ape DD), and those interested in the video form can follow my B station or video account (Program Ape DD) .
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, call it index.html
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
: IP address to be bound -
-p
: the access port to start -
-d
: the directory to serve -
-o
: the output level of the console
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 with the root directory /
, the service port is 9000, the binding address is 127.0.0.1, and the console output level is 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!
Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。