Java web basics 2-http protocol, javaweb
Java web basics 2-http protocol
Because java web is based on B/S, http is the most basic and important knowledge of java web. HTTP is used to define the format of communication between the client and the web server. Obtaining a web resource from a web server or receiving client requests from a web server must comply with a certain communication format, that is, the http protocol.
I. What is HTTP?
HTTP protocol is the cornerstone of learning java web and an essential foundation for managing and maintaining complex WEB sites. If you want to deeply understand the implementation of java web servers, you need to master it.
HTTP is short for hypertext transfer protocol (hypertext transfer protocol). It is an application layer protocol over TCP/IP. The default port is 80, defines the format and specifications for data exchange between a WEB browser and a WEB server.
HTTP is a stateless protocol because a web server does not save any information about the client. In the B/S architecture, web servers always open with fixed IP addresses and serve thousands of different browsers at the same time.
The HTTP protocol has two versions: HTTP/1.0 and HTTP/1.1. Currently, HTTP/1.1 is basically used. There is an important difference between them: In HTTP1.0 Protocol, only one web resource can be obtained after the client establishes a connection with the web server. HTTP1.1 protocol, allows the client to establish a connection with the web server and then obtain multiple web resources in a connection. That is, persistent connections.
Ii. Differences between persistent connections and non-persistent connections
A web page is also called a document. It is composed of objects. Simply put, objects are files, such as html files and image files. Most web pages contain a basic html file and multiple referenced objects. For example, if a web page contains a basic html file and two image files, the web page has three objects. The basic html file references the object through the object URL. The URL address consists of the Host Name of the server that stores the object and the path name of the object relative to the host. For example:
The c.csdnimg.cn in the http://c.csdnimg.cn/www/images/pic_foot_BNIA.png is the host name,
/Www/images/pic_foot_BNIA.png is the path name.
A non-persistent connection means that when the client requests a web page, the request/response to each object is sent through a separate TCP connection. For example, if a web page contains a basic html file and five JPEG image files. When a non-persistent connection is used, the user requests this page to establish six TCP connections.
Persistent connections are requests to a page. Even when multiple web pages on the same server are sent, they can be transmitted over a single persistent TCP connection. For example, in the preceding example, only a persistent TCP connection is required. In general, if a connection is not used after a certain period of time, the HTTP server will close the connection.
3. HTTP requests
After the browser of the client connects to the web server, it will request a web resource from the server, which is called an HTTP request sent by the client to the server. A complete HTTP request includes a request line, some message headers, and entity content. Some of the message headers and entity content are optional, the message header and entity content must be separated by blank lines.
1. Request Line
In the request line, you must note the following request methods:
POST, GET, HEAD, OPTIONS, DELETE, TRACE, PUT
We usually use POST and GET. If there are few others, we will not discuss them here.
Both POST and GET are used for the client to request a certain WEB resource from the server. They can all bring some data to the server, but they have some differences:
If the request method is GET, you can use? To the server. Multiple Data are separated by "&". For example:
GET/aaa/bbb? Name = ccc & password = maid HTTP/1.1
GET Mode features: the parameters attached to the URL address are limited, and the data capacity cannot exceed 1 K. As you can see, when using the GET request method, parameters are displayed in the input box of the browser, so they are visible.
When the request method is POST, you can send data to the server in the request's entity content, for example:
POST/user/loginHTTP/1.1
Host:
Content-Type: application/x-www-form-urlencoded
Content-Length: 28
Name = aaa & password = bbb
Post Mode features: There is no size limit on the amount of data transmitted, and the parameters are not displayed in the address box of the browser.
2. Message Header
The http Request Header must exist. It carries a lot of important information, such as the browser ID of the client. Below are common headers in HTTP requests:
Accept: text/html, image /*
Accept-Charset: ISO-8859-1
Accept-Encoding: gzip, compress
Accept-Language: en-us, zh-cn
Host: http://www.csdn.net: 80
If-Modified-Since: Tue, 11 Jul 2010 18:23:51 GMT
Referer: http: // http://www.csdn.net
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Cookie
Connection: close/Keep-Alive
Date: Tue, 11 Jul 2010 18:23:51 GMT
Iv. HTTP response
In response to HTTP requests, an HTTP response represents the data the server sends back to the client. It also includes three parts: a status line, several message headers, and entity content, some message headers and entity content are optional. A blank line is used as the interval between the message header and entity content.
The status code is the server's processing result for the request. It is a three-digit decimal number. The response status codes are divided into five categories:
100 ~ 199 |
The request is successfully received, and the client is required to submit the next request to complete the entire process. |
200 ~ 299 |
Indicates that the request is successfully received and the entire processing process has been completed. |
300 ~ 399 |
To complete the request, the customer needs to further refine the request. For example, the requested resource has moved a new address, which is usually 302, 307, and 304. For example, 304 redirection. |
400 ~ 499 |
Client request errors, such as 403 forbidden access and 404 cannot find resources |
500 ~ 599 |
An error occurs on the server. For example, if an exception is thrown by a server program, 500 is usually used. |
Like HTTP request headers, HTTP also has some common response headers, as shown below:
Location: http: // www.csdn.net
Server: apache tomcat
Content-Encoding: gzip
Content-Length: 80
Content-Language: zh-cn
Content-Type: text/html; charset = GB2312
Last-Modified: Tue, 11 Jul 2010 18:23:51 GMT
Refresh: 1, url = http://www.csdn.net
Content-Disposition: attachment; filename=aaa.zip
Transfer-Encoding: chunked
Set-Cookie: SS = Q0 = 5Lb_nQ; path =/aaa
Expires:-1
Cache-Control: no-cache
Pragma: no-cache
Connection: close/Keep-Alive
Date: Tue, 11 Jul 2000 18:23:51 GMT
Note that some common message headers can be used for both requests and responses, such:
Cache-Control: no-cache is used to specify whether the Cache is required
Pragma: no-cache is compatible with http 1.0. Cache-Control: no-cache is the newly added message header of http 1.1.
Connection: close/Keep-Alive, which controls whether a persistent Connection or a non-persistent Connection.
Date: Tue, 11Jul 2000 18:23:51 GMT