JavaWeb Summary (1)
When learning the client interface design of web applications, we already know that forming a basic web application requires a web server, web client browser, HTTP protocol and static HTML files.
Web server: receives client requests, and then returns some results to the client;
Browser: allows users to request a resource on the server, and displays the results of the request to the user;
HTML: tells the user how the browser Display content to users;
HTTP: The protocol used for communication between clients and servers on the Web;
HTTP
HTTP is a HyperText Transfer Protocol, a set of methods for computers to communicate on the network A rule. In the TCP/IP architecture, HTTP is an application layer protocol and is located on the top layer of the TCP/IP protocol. HTTP is a stateless protocol (no persistent link is required between the web browser [client] and the web server). The whole process is when a client sends a request (Request) to the server, and then the WEB server returns a response (Response). The link was then closed. HTTP follows the request/response (Request/Response) model, and all communication interactions are structured in a set of request and response models.
Transaction processing defined by HTTP consists of the following four steps
The client establishes a link with the Web server;
The client sends an HTTP request;
The server receives the client's HTTP request and generates an HTTP response postback;
Client Establish a link with the Web server;
Send request information
Once the TCP link is established, the Web browser will send a request command to the Web server. The HTTP protocol encapsulates request information from the client into an HTTP request, which includes a request line, a request header, a message body, and a blank line that separates the request header and message body;
Request line: The request line is an ASCII text line, consisting of three Tag composition: requested HTTP method, requested URL, HTTP version. Separate with spaces in the middle
In HTTP version 1.1, eight possible HTTP request methods are defined
Ⅰ GET: due to retrieving resources from the server;
Ⅱ POST: due to sending data to the server and requiring specification URL processing;
Ⅲ HEAD: Same as the GET method, the server only returns the status line and header, and does not return the request document;
Ⅳ PUT: Requests the server to save the request data as the new content of the specified URL;
Ⅴ DELETE: Request the server to delete the resource named in the URL;
Ⅵ OPTIONS: Request information about the request methods supported by the server;
Ⅶ CONNECT: A method that has been documented but not currently implemented, reserved for tunnel processing;
Among these methods , the most commonly used methods are GET and POST, and the other methods are not very commonly used by web developers.
Method
is the request for this method
POST
Enter the URL in the browser address bar;
Click the HTML link on the current web page;
In HTML form Set method='GET' in the form or not set it (the default is GET method submission), and submit the form;
GETGET
is usually used to send private information or large amounts of information, or upload document. If an application needs to modify or add data and sends a request via HTTP, the POST method should be used.
Request header:
The HTTP protocol uses the HTTP header to transmit the meta-information of the request. The HTTP header is a name/value pair separated by a colon. The colon is preceded by the name of the HTTP header, followed by the HTTP value. Common request headers consist of user agent information, acceptable formats, languages, and content encoding. This information is used to tell the server what the client is, what format of feedback information the client wants to get, etc.
Blank line:
Send a carriage return and a return character to notify the server that there are no more request headers.
Message body:
When the HTTP request contains a query string, if it is a GET method, the query string or form data is appended to the request line, then there is no content in the message body; if it is a POST method, the query string Or the form data is added to the message body.
Send back a response message
The web server parses the request, locates and reads the specified resource. Return files and other information to the client as an HTTP response. The HTTP response includes: status line, response headers, message body, and a blank line separating the message headers and response headers.
Status Line:
Every HTTP response begins with a status line. The status line consists of the HTTP protocol version, response status code, and response description, separated by spaces.
The response status code is a three-digit number, which is divided into the following groups:
Ⅰ 100~199: Information, request received, continue processing;
Ⅱ 200~299: Success, the behavior was successfully accepted , understanding and adoption;
Ⅲ 300~399: Redirect, further actions that must be performed in order to complete the request;
Ⅳ 400~499: Client error, the request contains a syntax error or the request cannot be implemented;
Ⅴ 500~ 599: Server error, the server cannot implement an obviously invalid request;
Each response status code has an associated string response description.
Response header:
The response header is the same as the request header. It is also a name/value pair separated by a colon. The colon is preceded by the name of the HTTP header and followed by the value of the HTTP header. Typical response headers include content description, content length, time stamp, server information, and time when the content was last modified. This information helps the client get what content was sent, the size of the content, and whether the data is newer than the previous response.
Among the response headers, the most important HTTP header is Content-Type, which specifies the MIME type. The MIME type tells the browser what type of data to accept, so that the browser knows how to display the data. This value is usually associated with the Accept in the HTTP request header
Blank Line:
The last response header is followed by a blank line, sending a carriage return and a backtrack, indicating that there are no more response headers to follow.
Message body:
HTML document to be sent to the client or other content to be displayed, etc. The web server puts the document information sent to the client in the message body.
Close the connection
After the HTTP response reaches the client, the browser first parses the status line in the HTTP response to see the status code of whether the request is successful. Then parse each response header, read the response message body, and render the message body on the browser page.
An HTML document may contain other resources that need to be loaded. The browser will recognize it and make additional requests for these resources. This process can continue to loop until all data is restored according to the format specified in the response header. to the page. After the data transmission is completed, the server closes the connection, which is a stateless protocol.