Java-based web server work mechanism (1)
A web server is also known as an HTTP server because it communicates with its clients and its customers, and these customers are usually browser. A Java-based web server uses two important classes: java.net.socket and java.net.serversocket, and communicate via HTTP messages. The beginning of this article will discuss HTTP and these two classes, followed, will explain the working mechanism of a simple web server application.
Hypertext Transfer Protocol (HTTP)
The HTTP protocol allows the server and client to receive and transmit data over the Internet. It is a request and response protocol ---- Client Send Request, the server responds to the request. HTTP uses a reliable TCP connection, the default TCP port is 80. The first edition of HTTP is 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 is simple to explain HTTP 1.1, which is still enough for the message sent by the web server application. If you are very interested, you can refer to RFC 2616 documentation.
With HTTP, the client initializes a transaction session by establishing a connection and sending an HTTP request, the server contacts the client or responds to a callback connection to the client. They can be interrupted. For example, when using a web browser, you can stop the file download process on the browser to stop the file download process, and turn off the HTTP connection with this web server.
HTTP request (Requests)
An HTTP Request contains three parts:
Method, URL, Agreement / Version request Baotou Request Headers entity package (Entity Body)
The example gives an example of an HTTP request:
Post / Servlet/default.jsp HTTP / 1.1
Accept: Text / Plain; Text / HTML
Accept-language: EN-GB
Connection: Keep-alive
Host: Localhost
Referr: 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 request is Method-Uri-Protocol / Version.
Post / Servlet/default.jsp HTTP / 1.1
The request is the POST method, the later / Servlet/default.jsp represents a URL address, and HTTP / 1.1 represents the version of the protocol.
The HTTP standard specification defines some request methods to use to give each HTTP request. HTTP 1.1 Support 7 Request: GET, POST, HEAD, OPTIONS, PUT, DELETE, and TRACE. GET and POST are the most common ways in Internet applications.
The URI completely specifically indicated an Internet resource. A URI is usually interpreted relative to the root directory of the server. Therefore, it always uses the symbol (/). A URL is actually a URI type. The protocol version represents the version of the HTTP protocol currently being used.
Request Header requests some useful client environments and entity (entity body) information. For example, it can include the length of the language and entity used by the browser. Each request header is separated by the CRLF (Route) sequence. In the previous HTTP request, the entity is a simple line below:
Lastname = franks & firstname = michael
In a typical HTTP request, this entity can easily become longer.
HTTP response (responses)
Similar to the request, an HTTP response also contains three parts:
Protocol-Status Code-Description Responds to the Response Headers entity (Entity Body)
Below is a simple example of HTTP response:
HTTP / 1.1 200 ok
Server: Microsoft-IIS / 4.0
Date: Mon, 3 Jan 1998 13:13:33 GMT
Content-Type: Text / HTML
Last-Modified: Mon, 11 Jan 1998 13:23:42 GMT
Content-Length: 112
Welcome to Brainy Software
body>
html>
The first line of response to the header and the request of the above request are similar. The first line tells us that the agreement is HTTP1.1, and the response request has been successful (200 means success), everything is OK.
In response to the header and the requesting header, some useful information is also included. The response entity is the content of the HTML. The head and entities are also separated by the CRLF sequence.
Socket class
Sockets are an endpoint for a network connection. It enables the application to read and write through the network. By sending and receiving byte streams on the connection, two software programs located in different computers can communicate with each other. In order to send a message to another, you need to know the IP address of the other machine and the socket port number. In Java, a socket is represented by the java.net.socket class.
In order to create a socket, you can use the Socket class constructor to complete. These constructors accept host names and ports:
Public socket (String Host, Int Port)
Host represents a remote computer name or an IP address, Port represents the port number of the remote application. For example, to connect to Yahoo.com at the 80-port, you need to construct the following Socket:
New Socket ("Yahoo.com", 80);
Once you have successfully created an instance of a Socket class, you can use it to send and accept bytes. To send byte streams, you must first call the GetputStream method of the Socket class to get a java.io.outputstream object. To send a text to a remote application, you often construct a java.io.PrintWriter object returned from the OutputStream object. To receive the byte stream connected to the other end, to call the GetInputStream method of the Socket class, the method is returned from java.io.inputstream.
The following block creates a socket, communicating with the local HTTP server (127.0.0.1 representative local), send an HTTP request, and then receives a response from the server. It creates a StringBuffer to save the response and print it to the console. Socket Socket = New Socket ("127.0.0.1", "8080");
OutputStream OS = Socket.getOutputStream ();
Boolean Autoflush = True;
PrintWriter Out = New PrintWriter (socket.getOutputStream (), Autoflush;
BufferedReader in = New BufferedReader
New INPUTSTREAMREADER (Socket.getInputStream ()));
// send an HTTP Request to the Web Server
Out.println ("Get /Index.jsp HTTP / 1.1");
Out.println ("Host: Localhost: 8080);
OUT.PRINTLN ("Connection: Close");
Out.println ();
// read the response
Boolean loop = True;
StringBuffer SB = New StringBuffer (8096);
While (loop) {
IF (in.ready ()) {
INT i = 0;
While (i! = - 1) {
i = in.read ();
Sb.Append ((char) i);
}
Loop = false;
}
Thread.currentthread (). SLEEP (50);
}
// Display the response to the out concele
System.out.println (sb.toString ());
Socket.close ();
To get an exact response from the server, you need to send an HTTP request to follow the HTTP protocol rule. If you read the above "Hypertext Transfer Protocol (HTTP)", you should be able to understand the code that just establishes Socket above.
Translated by Willpower, 2003.11.23