A Web server is also called an HTTP server because it communicates with its customers using the HTTP protocol, which is typically a browser. A java-based Web server uses two important classes: Java.net.Socket and Java.net.ServerSocket, and is communicated through HTTP messages. HTTP and these two classes are discussed at the beginning of this article, and later, the working mechanism of a simple Web server application is explained.
Hypertext Transfer Protocol (HTTP)
The HTTP protocol allows servers and clients to receive and send data over the Internet. It is a request and response protocol----The client sends a request, and the server responds to the request. HTTP uses a reliable TCP connection and the default TCP port is 80. The first version of HTTP was http/0.9 and then replaced by http/1.0. The current latest version is http/1.1, which is defined in the RPC2616 specification document.
This chapter simply spoke HTTP 1.1, which is sufficient for you to understand the message sent by the Web server application. If you are interested, you can refer to the RFC 2616 documentation.
With HTTP, the client initializes a transaction session by establishing a connection and sending an HTTP request, which the server contacts the client or responds to a callback connection to the client. All of them can break the connection. For example, when using a Web browser, you can stop the file download process by clicking the Stop button on the browser, effectively shutting down the HTTP connection to the Web server.
HTTP Request (Requests)
An HTTP request contains three parts:
method, URL, Protocol/version (method-uri-protocol/version)
Request Baotou Headers
Entity package (Entity body)
An example of an HTTP request is given below:
post/servlet/default.jsp http/1.1
Accept:text/plain; Text/html
Accept-language:en-gb
Connection:keep-alive
Host:localhost
Referer:http://localhost/ch8/senddetails.htm
user-agent:mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
Content-length:33
content-type:application/x-www-form-urlencoded
Accept-encoding:gzip, deflate
Lastname=franks&firstname=michael
The first line of the request is method-uri-protocol/version.
post/servlet/default.jsp http/1.1
The Post method is requested, and the following/servlet/default.jsp represents a URL address, http/1.1 represents the version of the Protocol.
The HTTP standard specification defines a number of request methods that are used for each HTTP request. HTTP 1.1 supports the request method in 7: Get, POST, head, OPTIONS, put, DELETE, and TRACE. Get and post are the most common two methods used in Internet applications.
The URI is a complete indication of an Internet resource. A URI is usually interpreted relative to the root directory of the server. Therefore, it always starts with the symbol (/). A URL is actually a URI type. The protocol version represents the version of the HTTP protocol that is currently in use.
The request header contains some useful information about the client environment and the requested entity (entity body) information. For example, it can contain the language and the length of the entity used by the browser, and so on. Each request header is separated by a CRLF (carriage return line wrap) sequence.
In the previous HTTP request, the entity was the following simple line:
Lastname=franks&firstname=michael
In a typical HTTP request, this entity can easily become longer.