Java Web Foundation Summary Two of the--http protocol

Source: Internet
Author: User

Java Web Foundation Summary of the two--http protocol

Since the Java Web is based on B/S, the HTTP protocol is the most basic and important knowledge of the Java Web. The HTTP protocol is used to define the format in which the client communicates with the Web server. Obtaining a Web resource from a Web server or accepting requests from a Web server to accept clients requires a certain communication format, which is the HTTP protocol.

A. What is the HTTP protocol?

The HTTP protocol is the cornerstone of Learning Java Web, but also a necessary foundation for managing and maintaining some complex web sites, and if you want to understand the Java Web server's implementation in depth, you need to master it.

HTTP is shorthand for the Hypertext Transfer Protocol (Hypertext Transfer Protocol), an application-layer protocol on top of the TCP/IP protocol, with the default port of 80, which defines the format and specification for exchanging data between a Web browser and a Web server.

The HTTP protocol is a stateless protocol because a Web server does not hold any information about the client. In the B/S architecture, the Web server is always open with a fixed IP address, while serving thousands of different browsers.

There are two versions of the HTTP protocol: http/1.0, http/1.1. The basic use of http/1.1 is now. There is an important difference between them: in the HTTP1.0 protocol, after a client has established a connection to a Web server, only one Web resource can be obtained. The HTTP1.1 protocol allows a client to obtain multiple Web resources on a connection after it has established a connection with the Web server. Which is often called persistent connections.

two. The difference between persistent and non-persistent connections

A Web page, also known as a document. is composed of objects, in short, the object is a file, such as HTML files, image files and so on. Most Web pages contain a basic HTML file and multiple reference objects. For example, a Web page contains a basic HTML file and 2 image files, then this web page has 6 objects. A basic HTML file references an object by its URL address. The URL address consists of two parts: the host name of the server that holds the object and the path name of the object relative to the host. For example:

The c.csdnimg.cn in Http://c.csdnimg.cn/www/images/pic_foot_BNIA.png is the hostname,

/www/images/pic_foot_bnia.png is the path name.

A non-persistent connection is when a client requests a Web page, and the request/response to each object is sent over a separate TCP connection. For example, if a Web page contains a basic HTML file and 5 JPEG picture files. When a non-persistent connection is used, a user requests this page and establishes 11 TCP connections.

A persistent connection is a request to a page that can be delivered on a single persistent TCP connection even when multiple Web pages are on the same server. For example above, you may need only a persistent TCP connection. In general, if a connection is not used at a certain time interval, the HTTP server shuts down the connection.

three. Requests for HTTP

after the client's browser connects to the Web server, a Web resource is requested from the server, which is called the client sends an HTTP request to the server. A complete HTTP request includes a request line, some message headers, and entity content, where some of the message headers and entity contents are optional, separated by a blank line between the message header and the entity content.

1. Request Line

The request line should be noted in the request method, mainly:

POST,, OPTIONS, DELETE, TRACE, PUT

We used to have: POST, GET. Other less encountered, here is not much to describe.

Post or get, which is used by the client to request a Web resource to the server, they can bring some data to the server, but they have a certain difference:

If the request method is get mode, you can bring the data to the server in the form of the requested URL address, and multiple data are separated by &, for example:

GET/AAA/BBB?NAME=CCC&PASSWORD=DDD http/1.1

The feature of Get mode is that the parameters accompanying the URL address are limited and the data capacity cannot exceed 1K. As you can see, when you use get requests, the parameters are displayed in the browser's input box, so it is visible.

When the request is in the post mode, data can be sent to the server in the requested 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: The amount of data transmitted is not limited by size, and parameters are not displayed in the browser's address box.

2. Message Headers

HTTP request header must exist, it carries a lot of important information, such as the client's browser identity, the following is the common header in the HTTP request:

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, Jul 18:23:51 GMT

referer:http://http://www.csdn.net

user-agent:mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)

Cookies

Connection:close/keep-alive

Date:tue, Jul 18:23:51 GMT

Four. HTTP response

As opposed to HTTP requests, an HTTP response represents the data that the server sends back to the client, which also includes three parts: a status line, several message headers, and entity content, some of which are optional, and a blank line between the message header and the entity content.

The status line needs to know is the status code, the status code represents the server to the request processing results, it is a three-bit decimal number. The response status code is divided into 5 categories, as follows:

100~199

Indicates a successful receive request that requires the client to continue submitting the next request in order to complete the process

200~299

Indicates a successful receive request and has completed the entire process, typically 200

300~399

To complete the request, the customer needs to refine the request further. For example, the requested resource has moved a new address, commonly used 302, 307, and 304. Like 304 redirects.

400~499

Client requests have errors, commonly used such as 403 of forbidden access and 404 of resources not found

500~599

Server error, such as server-side program throws an exception, commonly used 500.

Like HTTP request headers, HTTP also has some common response headers, as follows:

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, Jul 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, Jul 18:23:51 GMT

It is important to note that there are common message headers that can be used either for requests or for responses, such as:

Cache-control:no-cache used to specify whether a cache is required

Pragma:no-cache Pragma:no-cache compatible with HTTP 1.0, Cache-control:no-cache is the new message header for HTTP 1.1.

Connection:close/keep-alive This is whether the control is a persistent connection or a non-persistent connection.

Date:tue, 11Jul 18:23:51 GMT

Java Web Foundation Summary Two of the--http protocol

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.