|
Network Programming This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Tutorial /
Tutorial - Java Network Programming - Web Server ExampleIntroduction
The HyperText Transfer ProtocolThe HyperText Transfer Protocol (HTTP) is the set of rules for transferring files (for example, HTML documents, text, image, sound, video, and other multimedia files) between web clients and web servers. Relative to the TCP/IP suite of protocols, the HTTP is an application-layer protocol. The latest version of HTTP is HTTP 1.1.
HTTP communication can be divided into two separate parts, namely, HTTP requests and HTTP responses.
An HTTP request, which is made by a web client, contains information about a resource on the web server, and the action the client wishes the server to perform on the resource.
A web server fulfils an HTTP request by returning an HTTP response, which varies with the type of request and whether or not the request could be serviced.
To retrieve a web page from a certain HTTP server, an HTTP client simply sends a GET request to the server specifying a particular path and file name. The GET method is one type of requests of the HTTP application-level protocol. For example, the following HTTP request:
GET /index.html HTTP/1.0 tells the server that the client wants to get the file /index.html and that this is an HTTP version 1.0 request. The server will reply by sending the HTTP response header fields, followed by the file to the client, or an error message if it cannot provide the requested file.
The server’s reply contains the HTTP response code and description, possibly HTTP headers and content of the file. An example of an HTTP response is:
HTTP/1.0 200 OK
Content-Type: text/html
<HTML lang="zh">
[ … other content of HTML file … ]
In the first line of the above response, the number 200 is the response code (or status code) and the text ‘OK’ is the description of the code. Common HTTP response codes and their descriptions include:
There are many types of HTTP headers in a response. The headers are optional. One important type of headers is the content type, or MIME type. It specifies how the content that follows the headers is dealt with by the Web client. For example, a Web browser interprets content of type ‘text/html’ as a webpage and that of type ‘image/gif’ as a GIF image.
After the headers is an empty line, followed by the response body. In the above HTTP response, the response body is the content of the HTML file requested by the Web browser.
HTTP GET transaction (Source: http://oreilly.com/openbook/webclient/ch03.html)
Task 1: Compile the following Web Server program
Task 2: Prepare some simple web pages and images for the web server
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Network Programming</title> <style type="text/css" media="screen"> body {background-color:#87b2d5; text-align:center; color:#fff;font-family:Georgia, "Times New Roman", Times, serif;} p {margin:135px 0 35px 0; font-size:80px;} #countdown1 a {font-size:50px; text-decoration: none; color:#a4c7e4;} </style> </head> <body> <p>ouhk.edu.hk</p> <div id="countdown1"><a href="#">Network Programming</a></div> </body> </html> Task 3: Test the Web Server program
http://localhost/sample.jpg
http://localhost/indxe.html
Questions
Submission
|