HTTP message-->mvc

Source: Internet
Author: User
Tags 303 status code session id

Reference

Learning Web Development does not study the HTTP message, will be "Boxing do not practice, to the old one empty", you spend time on the confusion than you sank down to learn the HTTP time will certainly much more.


HTTP request message Anatomy

The HTTP request message consists of 3 parts ( request line + request Header + Request body ):



The following is an actual request message:



① is the request method, and get and post are the most common HTTP methods, in addition to the delete, HEAD, OPTIONS, PUT, TRACE. However, most current browsers only support get and post,spring 3.0 to provide a hiddenhttpmethodfilter that allows you to specify these special HTTP methods via the "_method" form parameter (in fact, submit the form via post). After the server is configured with Hiddenhttpmethodfilter, spring simulates the corresponding HTTP method based on the values specified by the _method parameter, so that the processing method can be mapped using these HTTP methods.

② to request the corresponding URL address, which consists of a complete request for the host property of the message header Url,③ is the protocol name and version number.

④ is the message header of HTTP, the message header contains several attributes, in the form of "attribute name: Attribute value", the service side obtains the client's information accordingly.

⑤ is the newspaper style, which encodes the component values in a page form into a formatted string by param1=value1&param2=value2 key-value pairs, which host data for multiple request parameters. Not only the style can pass the request parameters, the request URL can also be passed similar to "/chapter15/user.html?" Param1=value1&param2=value2 "method to pass the request parameter.

In contrast to the above request message, we decompose it further, and you can see a more detailed diagram of the structure:




Reference HttpWatch is a powerful web data analysis tool that is integrated into the Internet Explorer toolbar after installation. It is a necessary tool for Web application developers to capture complete information about requests and responses, including cookies, message headers, query parameters, and response messages, without a proxy server or some sophisticated network monitoring tools.



HTTP Request message Header Properties

What is the message header attribute? We might as well tell a little story.

Quote Fast to Noon, Zhang Sanfeng do not want to go to the canteen to eat, so call takeout: Boss, I want a [fish fragrant shredded pork], to 12:30 to send me over Oh, I was in Lake Lakes Company Research and Development department, called Zhang Sanfeng.



Here, you want to [fish fragrant pork] equivalent to the HTTP newspaper style, and "before 12:30 sent over", you call "Zhang San Fung" and other information is equivalent to the message header http. They are some ancillary information to help you and the restaurant owner to complete the transaction successfully.

Request HTTP messages and response HTTP messages have a number of message-off attributes, which are ancillary information to assist clients and server-side transactions.


Common HTTP Request message header Properties

Accept

The request message can tell the server client what type of response to accept via an "accept" message header property.

The following message header is equivalent to tell the server, my client can accept the response type is only plain text data ah, you do not send any other pictures ah, video ah come over, so I will take a break dishes ~ ~ ~:

Java code
    1. Accept:text/plain



The value of the Accept property can be one or more MIME-type values, for MIME types, please refer to: Http://en.wikipedia.org/wiki/MIME_type

Cookies

The client's cookie is passed through this message header attribute to the server! As shown below:

Java code
    1. Cookie: $Version =1; skin=new;jsessionid=5f4771183629c9834f8382e23be13c4c


How does the server know that multiple requests from the client are subordinate to a session? Notice that jsessionid=5f4771183629c9834f8382e23be13c4c wood in the background? It turns out that the Jsessionid value of the cookie attribute of the message header is associated with the HTTP request! (You can also add the session ID to the back of each URL by rewriting the URL, of course).


Referer

Indicates which URL this request came from, if you search for a business through Google Ads page, you are interested in this ad page, the mouse to send a request message to the merchant's website, this request message Referer message header attribute value is http://www.google.com.

Reference
Tang's Monk to the West.
The Tathagata asks: Does Lennon come from East Earth Datang?
Tang's Monk: Awesome! How did you know that?
Tathagata: Oh, I peeked at your referer ...



A lot of seemingly magical web monitoring software (such as the famous I Want), as long as you put a piece of JavaScript on your Web page, you can monitor traffic, the country's distribution of customer access reports and charts, the principle is through this referer and some other HTTP headers work.

Cache-control

Control of the cache, such as a request to be returned by the response of the client to be cached for one year, or do not want to be cached can be achieved through this message header.

The following settings are equivalent to letting the server return the response content of the corresponding request not in the client cache:

Java code
    1. Cache-control:no-cache



other Request message Header properties

See also: Http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

How to access the request packet header

Since the request packet header is sent by the client, the server is of course only able to read, the following is httpservletrequest some APIs for reading the request headers:

Java code
    1. Gets the property name in the request message
    2. Java.util.enumeration<java.lang.string> Getheadernames ();
    3. Gets the value of the message header property of the specified name
    4. Java.lang.String GetHeader (java.lang.String name)



Because some of the request message header properties are "too famous", HttpServletRequest provides them with the VIP API:

Java code
    1. Get the cookie in the message header (read the cookie's message header attribute)
    2. Cookie[] GetCookies ();
    3. Get client localization information (read Accept-language's message header properties)
    4. Java.util.Locale GetLocale ()
    5. Gets the length of the request style (read the Content-length property of the message header)
    6. int Getcontentlength ();



HttpServletRequest can be passed

Java code
    1. HttpSession getsession ()


The internal mechanism of obtaining the httpsession associated with the request is to obtain the corresponding HttpSession object in a session map on the server by reading the value of the Jsessionid of the cookie attribute in the request header. (So you don't think httpsession is mysterious, you can do a similar session management yourself)

HTTP Response message Anatomy

Response message Structure

HTTP response messages are also made up of three parts ( response line + response Head + response body ):



The following is an actual HTTP response message:



① message protocol and version;
② Status code and status description;
The ③ response message header is also composed of several attributes;
④ response to the style, that is, we really want "dry".

Response Status Code

Compared with the request message, the response message has a "response status code", which tells the client the processing result of the request in a "clear and clear" language.

The HTTP response status code consists of 5 segments:

    • 1XX message, the general is to tell the client, the request has been received, is processing, don't worry ...
    • 2XX processing success, generally said: The request received, I understand what you want, the request has been processed, has been completed and other information.
    • 3xx Redirect to a different location. It allows the client to initiate a request again to complete the processing.
    • 4XX processing An error occurred, the responsibility of the client, such as the client's request for a non-existent resource, the client is not authorized, access is forbidden, etc.
    • 5XX processing error, the responsibility on the server side, such as the server throws an exception, routing error, HTTP version is not supported and so on.



Here are a few common status codes:

OK

What you would most like to see is the process of success!

303 See other

I redirect you to other pages where the URL of the target tells you by the location of the message header.

Quote Wukong: Master to a peach bar, walked a day
Tang's Monk: I don't have any peaches! Go to Heavenly Queen and find it.



304 Not Modified

Tell the client that you requested this resource to the last time you obtained, and did not change, you directly with your local cache, I am very busy oh, you can not bother me!

404 Not Found

The last thing you want to see is that you can't find the page. If you find a page on Google, click on this link to return 404, that this page has been deleted by the site, Google's record is just a good memory.

Internal Server Error

See this error, you should check the service side of the log, must throw a bunch of anomalies, don't sleep, get up and change the bug!


Other status codes see: Http://en.wikipedia.org/wiki/List_of_HTTP_status_codes


Some response codes are generated automatically by the Web application server. You can set the status code via the HttpServletResponse API:

Java code
    1. To set the status code, the status code is pre-defined in HttpServletResponse by a series of constants, such as Sc_accepted,sc_ok
    2. void SetStatus (int SC)



Common HTTP Response message header properties

Cache-control

After the response is output to the client, the service side tells the client how to control the cache of the response content through the header.

Below, the settings allow the client to cache the response content for 3,600 seconds, that is, within 3,600 seconds, if the customer accesses the resource again, directly from the client's cache to return content to the customer, do not get from the server (of course, this function is implemented by the client, the server only through this property prompts the client " Should do ", do not do, or decide on the client, if you are claiming to support the HTTP client, then it should be implemented."

Java code
    1. cache-control:max-age=3600



ETag

A message header attribute that represents the version of a server-side resource (such as a page), and if a server resource changes, the ETag changes accordingly. It is a useful complement to Cache-control, which allows the client to "smarter" handle when to fetch resources from the server and when it can return a response directly from the cache.

For a description of the ETag, you can refer to: Http://en.wikipedia.org/wiki/HTTP_ETag.
Spring 3.0 is also dedicated to provide a org.springframework.web.filter.ShallowEtagHeaderFilter (the implementation of the principle is very simple, the content of the JSP output MD5, so that the content changes the etag corresponding change), to generate the response of the etag, because For this stuff really can help reduce the interaction of requests and responses.

Here is an etag:

Java code
    1. ETag: "737060cd8c284d8af7ad3082f209582d"



Location

We make the page in the JSP redirect to a page, in fact, let the client send a request to a page, this need to redirect to the A page URL, in fact, through the response message header Location property to inform the client, such as the next message header properties, The client will be redirect to the first page of Iteye:

Java code
    1. Location:http://www.iteye.com



Set-cookie

The server can set the client's cookie, the principle of which is implemented through this response message header properties:

Java code
    1. Set-cookie:userid=johndoe; max-age=3600; version=1




other HTTP response message header properties

For additional HTTP response header messages, see: Http://en.wikipedia.org/wiki/List_of_HTTP_header_fields


how to write an HTTP request message header

The properties of the response headers can be written by the HttpServletResponse API on the server side:

Java code
    1. Add a response message header property
    2. void SetHeader (string name, String value)



Like Cookie,location These responses are blessed, and HttpServletResponse provides them with the VIP version of the API:

Java code
    1. Add Cookie Message Header properties
    2. void Addcookie (Cookie cookie)
    3. It will not only set the response header of the location, but also generate a 303 status code.
    4. void Sendredirect (String location)



These articles are excerpted from my "Spring 3.x enterprise Application Development Practice", I will be in the form of a serial, in succession here. You are welcome to discuss.

HTTP message-->mvc

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.