Java Tools Tutorials - Herong's Tutorial Examples - v6.24, by Herong Yang
jwebserver - Java Web Server
This chapter provides provides a tutorial example on how to use 'jwebserver' command to launch the Java Simple Web Server.
What Is "jwebserver"? - "jwebserver" is the command that launches the Java Simple Web Server provided in JDK since Java 18.
Main features of the Java Simple Web Server are:
If you have %java_home%\bin directory included in "path" the environment variable, you can run "jwebserver --help" to get the usage information:
herong> jwebserver --help 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. To stop the server, press Ctrl + C.
Here is what I did to try the "jwebserver" tool.
1. Create a sub-directory "www" with the following files:
herong> cd www herong> dir index.html sample.json sample.png sample.xml
2. Start the Java Simple Web Server:
herong> jwebserver -o verbose Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::". Serving /Users/herong/www and subdirectories on 127.0.0.1 port 8000 URL http://127.0.0.1:8000/
3. Request the default page in a Web browser at http://127.0.0.1:8000/. I see "Hello world!" displayed. "jwebserver" printed the following logging messages:
127.0.0.1 - - [20/May/2023:08:21:26 -0400] "GET / HTTP/1.1" 200 - Resource requested: /Users/herong/www > Accept-encoding: gzip, deflate > Accept: text/html,application/xhtml+xml,application/xml;... > Connection: keep-alive > Host: 127.0.0.1:8000 > User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) ... > Accept-language: en-us > Upgrade-insecure-requests: 1 > < Date: Sat, 20 May 2023 12:21:26 GMT < Last-modified: Sat, 20 May 2023 12:21:22 GMT < Content-type: text/html < Content-length: 74
4. Request the XML sample page in a Web browser at http://127.0.0.1:8000/sample.xml. I see the XML document displayed:
<html> <head> <title>Hello</title> </head> <body>Hello world!</body> </html>
5. Request the PNG sample page in a Web browser at http://127.0.0.1:8000/sample.png. I see the sample image displayed.
Note that "jwebserver" is located in the jdk.httpserver module. The Java Simple Web Server is based on the web server implementation in the com.sun.net.httpserver package and the com.sun.net.httpserver.SimpleFileServer class.
You can actually launch the Java Simple Web Server directly from the jdk.httpserver module:
herong> java -m jdk.httpserver Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::". Serving /Users/herong/www and subdirectories on 127.0.0.1 port 8000 URL http://127.0.0.1:8000/
Table of Contents
javac - The Java Program Compiler
java - The Java Program Launcher
jpackage - Binary Package Builder
javadoc - The Java Document Generator
jdeps - The Java Class Dependency Analyzer
jdeprscan - The Java Deprecated API Scanner
jcmd - The JVM Diagnostic Tool
jconsole - Java Monitoring and Management Console
jstat - JVM Statistics Monitoring Tool
jhsdb - The Java HotSpot Debugger
jvisualvm (Java VisualVM) - JVM Visual Tool
javap - The Java Class File Disassembler
keytool - Public Key Certificate Tool
jrunscript - Script Code Shell
serialver - serialVersionUID Generator
jaotc - Java Ahead-Of-Time Compiler
native2ascii - Native-to-ASCII Encoding Converter