Java Reference
In-Depth Information
When this program is launched the port that web server will listen at must be specified.
Normally web servers listen at port 80 . However, there may already be a web server run-
ning at port 80 . If this is the case a higher port number such as 8080 or 8081 should be
used. The server is started as follows:
SimpleWebServer 8080
This will start the web server on port 8080 . The above command simply shows the
abstract format to call this recipe, with the appropriate parameters. For exact information on
how to run this recipe refer to Appendix B, C, or D, depending on the operating system you
are using. If something already has that port in use then a BindingException error
message will be shown, as seen in Listing 1.5.
Listing 1.5: Port Already in Use
java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
at com.heatonresearch.httprecipes.ch1.recipe2.WebServer.
<init>(WebServer.java:51)
at com.heatonresearch.httprecipes.ch1.recipe2.WebServer.
main(WebServer.java:290)
If the web server is started properly, and no error occurs, there should be no output. The
web server is now waiting for a connection. Connecting to the web server is easy. Use any
web browser and access the following URL:
http://localhost:8080/
No matter what request is sent to this web server, it will produce a page that says Hello
World. The output from Recipe 1.1 is shown in Figure 1.7.
Search WWH ::




Custom Search