Java Reference
In-Depth Information
As can be seen from the above listing, at first glance, response headers look nearly the
same as request headers. However, look at the first line.
Although the first line is space delimited as in the request, the information is different.
The first line of HTTP response headers contains the HTTP version and status information
about the response. The HTTP version is reported as 1.1, and the status code, 200 , means
“OK,” there was no error. Also, this is where the famous error code 404 (page not found)
comes from.
Error codes can be grouped according to the digit in their hundreds position:
• 1xx: Informational - Request received, continuing process
• 2xx: Success - The action was successfully received, understood, and accepted
• 3xx: Redirection - Further action must be taken in order to complete the request
• 4xx: Client Error - The request contains bad syntax or cannot be fulfilled
• 5xx: Server Error - The server failed to fulfill an apparently valid request
Immediately following the headers will be a blank line, just as was the case with HTTP
requests. Following the blank line delimiter will be the data that was requested. It will be of
the length specified in the Content-Length header . The Content-Length
header in Listing 1.4 indicates a length of 1,289 bytes. For a list of HTTP codes, refer to Ap-
pendix G, “HTTP Response Codes.”
Recipes
In this chapter, the structure of HTTP requests and responses was examined. As shown
in Listing 1.3 and 1.4, the structure of HTTP is not terribly complex. As a result, it is relatively
easy to create a web server. This is what the two recipes in this chapter will deal with. The
first Recipe, 1.1, will show how to create a really simple web server. Next, Recipe 1.2 will
show how to extend the simple web server to use HTML and image files, just as a regular
web server would.
The recipes in this chapter make use of Java sockets. Sockets are the lowest level that an
application programmer will usually get to the Internet connection. The socket level allows a
web server to be created. After this chapter, all Java recipes will make use of the Java HTTP
classes. If desired, HTTP programming could be performed at the socket level; however, us-
ing the Java HTTP classes will get the needed functionality, without the complexity of dealing
directly with sockets.
Recipe #1.1: A Simple Web Server
The first recipe is a “Hello World” program of sorts. Recipe 1.1 is a web server. Its pur-
pose is to show how to serve web pages from your program. This very simple program only
serves one web page. This page simply says “Hello World”.
Search WWH ::




Custom Search