Servlet gets the method that the parameters in the Ajax post request are transmitted in the form of form data and request payload
This paper describes the method of servlet obtaining the parameters in Ajax post request and transmitting them in the form of form data and request payload. Share with you for your reference, as follows:
In the HTTP request, if it is a get request, the form parameters are attached to the URL in the form of name = value & name1 = value1. If it is a post request, the form parameters are in the request body as well as in the form of name = value & name1 = value1. Through the developer tools of chrome, you can see the following (here is a readable form, not the request format of the real HTTP request protocol):
Get request:
Post request:
Note that the content type of the post request is application / x-www-form-urlencoded, and the parameter is in the request body, that is, the form data in the above request.
In a servlet, you can use request Get form parameters in the form of getparameter (name).
If you use native Ajax post requests:
Through the developer tool of chrome, you can see the request header as follows:
Note that the content type of the request is text / plain; Charset = UTF-8, and the request form parameters are in requestpayload.
Then the servlet passes request Getparameter (name) is empty. Why? How to obtain such parameters?
In order to understand this problem, I checked some information and read Tomcat 7 0.53 about the source code of request parameter processing, I finally understand what's going on.
When submitting an HTTP post form request, the content type used is application / x-www-form-urlencoded. For post requests using native Ajax, if the request header is not specified, the content type used by default is text / plain; charset=UTF-8。
Tomcat has made "special processing" for content type multipart / form data (file upload) and application / x-www-form-urlencoded (post request). Let's take a look at the relevant processing codes.
The implementation class of HttpServletRequest class of Tomcat is org apache. catalina. connector. Request (actually org. Apache. Coyote. Request), and its method for processing request parameters is protected void parseparameters(). In this method, the processing codes for content type multipart / form data (file upload) and application / x-www-form-urlencoded (post request) are as follows:
As can be seen from the above code, the post request with content type other than application / x-www-form-urlencoded will not read the request body data and process the corresponding parameters, that is, the form data will not be parsed and put into the request parameter map. So through request Getparameter (name) cannot be obtained.
So how do we get the submitted parameters?
Of course, the most primitive method is used to read the input stream to obtain, as shown below:
Of course, the post request with application / x-www-form-urlencoded set can also be obtained in this way.
Therefore, when using native Ajax post requests, you need to explicitly set the request header, that is:
In addition, if you use jQuery, I use 1.11 0 to test, $ Ajax post requests do not need to explicitly set the request header, and I have not personally tested other versions. Believe in 1.11 Versions after 0 also do not need to be set. But not before. This has not been tested.
Postscript:
Only recently did I really understand why the server does special processing for form submission and file upload, because the data submitted by the form is name value pair, and the content type is application / x-www-form-urlencoded, while the file upload server needs special processing, The data format of ordinary post requests (content type is not application / x-www-form-urlencoded) is not fixed, and it is not necessarily a name value pair. Therefore, the server cannot know the specific processing method, so it can only be parsed by obtaining the original data stream.
When jQuery executes a post request, it sets the content type to application / x-www-form-urlencoded, so the server can parse correctly. When using a native Ajax request, if the content type is not displayed, it defaults to text / plain. At this time, the server does not know how to parse the data, Therefore, the request data can only be parsed by obtaining the original data stream.
For more information about Java algorithms, readers who are interested can see the topics on this site: summary of Java network programming skills, tutorial on Java data structure and algorithms, summary of Java DOM node skills, summary of java file and directory operation skills, and summary of Java cache operation skills