Spring Source Analysis Spring-web http detailed

Source: Internet
Author: User

Spring-web is the foundation of Spring WEBMVC, and it functions as follows:

1. Package Request requests and response responses to the client end/server end of the HTTP protocol, such as Json,rss,xml.

2. Remote invocation includes JAXWS, Caucho, Httpinvoker

3. Web-related Accept/bind/client/context/filter/jsf/method/multipart

Let's start with the encapsulation in the HTTP protocol:

First look at the basics of http: (From: http://blog.csdn.net/lxzo123/article/details/6708035 HTTP protocol--Request format and method)

One, HTTP request format

When the browser makes a request to the Web server, it passes a block of data to the server, which is the request information, and the HTTP request information consists of 3 parts:

① Request Method URI Protocol/version

② Requests Header (request header)

③ Request Body

The following is an example of an HTTP request:

Get/sample. JSP http/1.1

accept:image/gif.image/jpeg,*/*

Accept-language:zh-cn

Connection:keep-alive

Host:localhost

user-agent:mozila/4.0 (compatible; MSIE5.01; Window NT5.0)

Accept-encoding:gzip,deflate

username=jinqiao&password=1234

(1) Request method URI Protocol/version

The first line of the request is "Method URL protocol Version": Get/sample.jsp http/1.1

In the code above, "GET" represents the request method, "/sample.jsp" represents the URI, "http/1.1 represents the version of the Protocol and Protocol."

HTTP requests can use a variety of request methods, depending on the HTTP standard. For example: HTTP1.1 supports 7 methods of request: GET, POST, HEAD, OPTIONS, PUT, delete, and Tarce. In Internet applications, the most common method is get and post.

The URL completely specifies the network resource to be accessed, usually with a relative directory relative to the root of the server, always beginning with a "/", and finally, the version of the Protocol that declares the use of HTTP during communication.

(2) Requesting header (Request header)

The request header contains many useful information about the client environment and the request body. For example, the request header can declare the language used by the browser, the length of the request body, and so on.

accept:image/gif.image/jpeg.*/*

Accept-language:zh-cn

Connection:keep-alive

Host:localhost

user-agent:mozila/4.0 (Compatible:msie5.01:windows NT5.0)

Accept-encoding:gzip,deflate.

(3) Request body

between the request header and the request body is a blank line, which is very important, which indicates that the request header has ended, followed by the request body . The request body can contain query string information submitted by the customer:

username=jinqiao&password=1234

In the HTTP request for the example above, the body of the request has only one line of content. Of course, in real-world applications, the HTTP request body can contain more content.

Iv. http request method: GET POST

1) Get method

The Get method is the default HTTP request method, and we routinely use the Get method to submit form data, but the form data submitted with the Get method is simply encoded, and it is sent to the Web server as part of the URL, so If you use the Get method to submit form data, there is a security risk. For example

Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB

From the URL request above, it is easy to identify what the form submits. (? ) In addition, because the data submitted by the Get method is part of the URL request, the amount of data submitted cannot be too large

2) Post method

The Post method is an alternative to the Get method, which is primarily to submit form data to the Web server, especially large batches of data. The Post method overcomes some of the drawbacks of the Get method. When submitting form data through the Post method, the data is not sent as part of the URL request but as standard data to the Web server, which overcomes the drawback that the information in the Get method is not confidential and the amount of data is too small. Therefore, for security reasons and respect for user privacy, the Post method is usually used for form submission.

From a programmatic point of view, if a user submits data through a GET method, the data is stored in the QUERY_STRING environment variable, and the data submitted by the Post method can be obtained from the standard input stream.

Anyway

HTTP Basics
1. HttpRequest inherits from Httpmessage defines the basic format of the HTTP request: Header, method, URL, where HttpMethod defines all the methods of the HTTP protocol:

 Public enum HttpMethod {    GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE}

2. Responseentity inherits from Httpentity defines the format of the HTTP response: header, body, status, where Httpstatus is an enumeration class that defines the response status codes for 1XX, 2xx, 3xx, 4xx, 5xx

3. MediaType inherits from MimeType and defines acceptable request file types.

HTTP Client

1. Clienthttprequest is the interface requested by the client, and its abstract class is abstractclienthttprequest;simplestreamingclienthttprequest ( Use JKD's own capabilities to perform streaming requests), Httpcomponentsstreamingclienthttprequest (using Apache components to process requests), Abstractbufferingclienthttprequest (abstract buffer) inherits from Abstractclienthttprequest abstract class Bufferingclienthttprequestwrapper (simple clientrequest implementation), Httpcomponentsclienthttprequest (Apache Request processing implementation of components), Simplebufferingclienthttprequest (using the JDK's Self-feature HTTP request processing implementation) inherits from the abstract class abstractbufferingclienthttprequest.

From the above diagram, we can clearly see the genealogy relationship of Clienthttprequest and its realization class.

2. Clienthttpresponse is the interface of the client response, Bufferingclienthttpresponsewrapper is a simple implementation of clienthttpresponse, which reads the request's body into memory, Therefore, the GetBody () method is supported multiple times, and the abstract class of Clienthttpresponse is Abstractclienthttpresponse; the abstract class has three implementation classes. Where Simpleclienthttpresponse is implemented using the JKD feature, you can use the Simplebufferingclienthttprequest execute () method or the Simplestreamingclienthttprequest execute () method to get to the object; Httpcomponentsclienthttpresponse is using Apache The return object of the Httpcomponentsclienthttprequest request in the components component; Httpcomponentsasyncclienthttpresponse is using Apache The return object of the Httpcomponentsasyncclienthttprequest request in the Componets component.

3. Asynchronous request Asyncclienthttprequest is an interface that simplestreamingasyncclienthttprequest asynchronous stream processing requests implemented using the JDK's own functionality can be obtained by org.springframework.http.client.SimpleClientHttpRequestFactory createrequest method; abstractbufferingasyncclienthttprequ EST is an abstract class of asyncclienthttprequest interfaces that is cached in a byte array before the request is sent, and Simplebufferingasyncclienthttprequest is the implementation class for the above abstract class. It uses the JKD custom feature to perform cache requests. The object can be obtained by org.springframework.http.client.SimpleClientHttpRequestFactory the Createrequest method;

Httpcomponentsasyncclienthttprequest is the implementation class for the above abstract class, which uses the httpclient of the Apache Httpcomponents component to execute the request. This object can be obtained by Org.springframework.http.client.HttpComponentsClientHttpRequestFactory's Createrequest method.

4. The asynchronous response Httpcomponentsasyncclienthttpresponse inherits from the Abstractclienthttpresponse abstract class and indirectly inherits from the Clienthttpresponse interface.

Note: Request requests are typically generated by the request factory requestfactory.

HTTP Server

1. Servletserverhttprequest inherits and implements the ServerHTTPRequest interface, encapsulates the HttpServletRequest, and supports Asynchrony.

2. Servletserverhttpresponse inherits and implements the Serverhttpresponse interface, encapsulating the HttpServletResponse.

3. Asynchronous processing is done by Servletserverhttpasyncrequestcontrol, which inherits and implements the Serverhttpasyncrequestcontrol interface. It processes the HTTQ request asynchronously before the response is closed.

http Converter

First look at its hierarchy chart:

By default, there are some of the following Httpmessageconverter:

      • bytearrayhttpmessageconverter  –  convert byte array
      • stringhttpmessageconverter  – Convert String type
      • resourcehttpmessageconverter  – Convert any type of 8-byte stream to Org.springframework.core.io.Resource
      • sourcehttpmessageconverter  – convert   Javax.xml.transform.Source
      • formhttpmessageconverter  – Conversion form to/from multivaluemap< String, string> type .
      • jaxb2rootelementhttpmessageconverter  – convert Java objects to/from XML format (only applicable in classpath file increments JAXB2)
      • mappingjackson2httpmessageconverter  – conversion JSON type (only available in Clsspath jackson2)
      • mappingjacksonhttpmessageconverter  – conversion JSON type (only available for Jackson in Clsspath)
      • Atomfeedhttpmessageconverter  – convert Atom feeds (only for Rome in Clsspath)
      • Rsschannelhttpmessageconverter  – convert rss feeds  (only for Rome in Clsspath)

Summary:

Spring-web is the foundation of Spring Webmvc, and Spring-web is based on the Spring-web HTTP module, which contains four modules:

1. HTTP encapsulation base such as HttpRequest, HttpResponse, Httpstatus, httpheader, etc.;

2. Request requests and responses from client side;

3. Request requests and responses from server side;

4. Format conversion, such as Json,rss,xml.

This module can be learned in conjunction with the HTTP protocol and servlet to understand how Spring's encapsulation of HTTP is implemented.

Spring Source Analysis Spring-web http detailed

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.