Basic Principles of HTTP protocol and principles of web programs

Source: Internet
Author: User

The first thing to note is:
The interaction between HTTP Protocol must be understood. HTTP is based on TCP, application layer protocol, and connection.
HTTP is a request/response mode-based stateless protocol (Request/response based, stateless protocol). Stateless means that I sent you a request first, then I send another request, and the server does not know that you have previously sent a request. sometimes it is necessary to overcome this problem. For example, after logging on, I can access some resources.

The difference between http1.0 and http1.1 lies in the number of established connections, and the number of requests is the same.
The method actually corresponds to the crud in the database.
Get: Query
Put: Save
Post: Update
Delete method: Delete

How are they connected? The connection is actually the same as that of TCP: the server listens after it is started to check whether the client has connected to it. This is the principle of the HTTP connection.

 

Differences between http1.0 and http1.1:

In http1.0, when a connection is established, the browser sends a request, the server responds to a message, and the connection is closed. When the browser requests the next time, it needs to establish a new connection. Obviously, this communication method requires a large overhead. Early web pages usually only contain HTML text, so even if the connection overhead is large, it will not have a big impact. Nowadays, web pages often contain multiple types of resources (images, animations, sounds, etc.). Each time a resource is obtained, a connection is established, which increases the overhead of the HTTP server, information on the Internet is blocked.

In http1.1, a persistent connection (persistent connections) mechanism is provided and used as the default action for establishing connections in http1.1. Through this connection, the browser can send a request and receive a response after establishing a connection, and then send the request again. In addition, the client can also send pipeline requests. That is to say, the client can send multiple requests consecutively without waiting for the arrival of each response. The concept of timeout is as follows, when you do not connect to the server for a long time, the server automatically disconnects

A web site may receive millions of user requests every day. To improve system efficiency, HTTP 1.0 requires that the browser and server only maintain a short connection, each browser request requires a TCP connection with the server. The server disconnects the TCP connection immediately after processing the request. The server does not track or record the previous request. However, this also causes some performance defects. For example, a webpage file containing many images does not contain real image data content, the URL of these images is specified. When a web browser accesses this webpage file, the browser must first send a request for this webpage file, when the browser parses the HTML content in the webpage document returned by the Web server, after the Image Tag is found, the browser sends a request to the server again to download image data based on the URL specified by the src attribute in the label.


HTTP request and HTTP response:

An HTTP request consists of three parts: the request line, the message header, and the request body.

The request line starts with a method symbol, followed by the request URI and Protocol version, ending with CRLF. Request lines are separated by spaces. In addition to the ending CRLF, separate CR or lf characters are not allowed. The format is as follows:
-Method Request-Uri http-version CRLF
Method indicates the request method. Request-Uri is a unified resource identifier that identifies the resource to be requested. http-version indicates the HTTP protocol version of the request, and CRLF indicates the carriage return line break. For example:
-Get/test.html HTTP/1.1 (CRLF)

The two most important methods are get and post.

The get method is used to obtain information about the resource identified by request-Uri. The common form is:
-Get request-Uri HTTP/1.1, all in the request line

The post method is used to send requests to the server, requiring the server to accept the data attached to the request. The post method is used most frequently when submitting a form.
• The example of using the POST method to submit a form is placed in the Request body
Post/login. jsp HTTP/1.1 (CRLF)
Accept: image/GIF (CRLF )(....)
HOST: www.sample.com (CRLF )(....)
....
Cache-control: NO = cache (CRLF)
(CRLF) note that there are two carriage return line breaks, indicating that the following is the request body and the above is the request header.
Username = Hi & Password = 123456

Another common method is head.

The head method is almost the same as the get method. The difference is that the head method only requests the message header, not the complete content. For the response part of a head request, the information contained in the HTTP header is the same as the information obtained through the GET request. With this method, you do not need to transmit the content of the entire resource to obtain the information of the resource identified by request-Uri. This method is usually used to test the validity, accessibility, and recent updates of hyperlinks.

 

Similar to HTTP requests, an HTTP response is composed of three parts: Status line, message header, and response body.

The status line consists of the Protocol version, status code in numeric form, and corresponding status descriptions. Each element is separated by a space, except for the ending CRLF (carriage return) sequence, cr or lf characters are not allowed. The format is as follows:
-Http-version status-code reason-phrase CRLF
• Http-version indicates the HTTP protocol version of the server, status-code indicates the response code sent back by the server, reason-phrase indicates the text description of the status code, and CRLF indicates the carriage return line break, for example:
-HTTP/1.1 200 OK (CRLF)

 

HTTP message:

An HTTP message consists of a client-to-server request and a server-to-client response. The request message and Response Message are composed of the starting line, the message header (optional), empty lines (only CRLF lines), and the message body (optional.
• For request messages, the start line is the request line, and for response messages, the start line is the status line.

 

Note that:

Difference between get and post methods:
1) different results are displayed in the browser address bar (Representation)
2) the real reason is that the form of the request sent to the server is different.

GET request format:

POST request format:

 

When uploading files through a browser, you must use the POST method instead of the get method. The reason is: the GET request requires the length of its information, but there is no limit on post.

 

Java Web applications can be deployed in a variety of ways:
You can configure various project startup methods,

1) <context Path = "/test" docbase = "D: /Java Web/shengsiyuan/test/webroot "reloadable =" true "/> This method should be better. the path is a logical path, the docbase is a physical path, and the path is the context path of the Project. Note that it has no relationship with the project name.
2) The test in Tomcat's webapp is essentially webroot when it is deployed using myeclipse. We do not recommend using this method for deployment, which is too dependent on IDE, some companies do not use myeclipse and have a weakness. It is a waste of time to deploy them each time.
3) copy the webroot directory to the webapp and modify the name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.