How the servlet gets the parameters of the POST request

Source: Internet
Author: User

Usually we use Request.getparameter (parameter name) to get the arguments above or Ajax.data submitted by the URL, but for example, using the Httppost.setentry in HttpClient () The method submits the parameter is not the parameter name, uses the Request.getparameter (parameter name) cannot obtain the parameter value, we may use the Request.getinputstream () (Io flow) method to obtain the content in the body, Gets a JSON string, then uses the Jsonobject.fromobject (JSON string), and resolves to the values of each parameter in it.

Suppose we get the content of both URL-submitted parameters, there are httpclient in the Httppost.setentry () of the method submitted parameters, this time how to get the value of the parameter?

The normal idea should be: First use Request.getparameter (parameter name) to get the parameters above the URL,

Then use Request.getinputstream () (IO stream) to get the contents of the body without parameter names

The parameter contents obtained using IO Stream are found to be null

The reason: servlet specification: The data in the request body is populated in the parameter collection when the following conditions are met

1: is a HTTP/HTTPS request

2: The request method is post

3: Request type (Content-type) is application/x-www-form-urlencoded

4:servlet called the GetParameter series method

If the above conditions are not met at the same time, the associated form data is not set in the parameter collection of the request.

The relevant data can be accessed through Request.getinputstream (). Conversely, if the above conditions are met,

The related form data will no longer be read by Request.getinputstream ().

According to the description of this specification, when we call the Request.getparameter (parameter name) method,

The data (the request body) submitted through the Httppost.setentry () in HttpClient is populated into the parameter collection.

So the subsequent fetch of the data stream through the Request.getinputstream is empty.

The final solution to this problem is to put the Request.getparameter (parameter name);

After Request.getinputstream, this avoids the servlet specification, and can get the desired parameter values normally.

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.