The form form in HTML has a key attribute enctype=application/x-www-form-urlencoded or Multipart/form-data.
1, enctype="application/x-www-form-urlencoded" is the default encoding method, when the data is submitted in this way, the content of the HTTP message is:
HTML code
- <span style="Font-size:small;" >post/post_test.php http/1.1
- Accept-language:zh-cn
- user-agent:mozilla/4.0
- content-type:application/x-www-form-urlencoded
- host:192.168.12.102
- Content-length:42
- Connection:keep-alive
- Cache-control:no-cache
- title=test&content=%b3%ac%bc%b6%c5%ae%c9%fa&submit=post+article
- </span>
The Servlet API provides support for decoding this encoding, and only calls the GetParameter () method in the ServletRequest class to get the data submitted in the form.
2, in the transmission of large data volumes of binary data, the encoding must be set to Enctype= "Multipart/form-data", when the data is submitted in this way, the content of the HTTP message is:
HTML code
- <span style="Font-size:small;" >post/post_test.php? t=1 http/1.1
- Accept-language:zh-cn
- user-agent:mozilla/4.0
- Content-type:multipart/form-data; boundary=---------------------------7dbf514701e8
- Accept-encoding:gzip, deflate
- host:192.168.12.102
- content-length:345
- Connection:keep-alive
- Cache-control:no-cache
- -----------------------------7dbf514701e8
- Content-disposition:form-data; name="title"
- Test
- -----------------------------7dbf514701e8
- Content-disposition:form-data; name="content"
- ....
- -----------------------------7dbf514701e8
- Content-disposition:form-data; name="Submit"
- Post article
- -----------------------------7dbf514701e8--</span>
If data is submitted in this way, the submitted data will be obtained using Request.getinputstream () or Request.getreader () , and Request.getparameter () will not get the submitted data.
Finally note that the three methods of Request.getparameter (), Request.getinputstream (), and Request.getreader () are conflicting because the stream can only be read once.
Like what:
When the form form content is enctype=application/x-www-form-urlencoded encoded, the data is first fetched by calling the Request.getparameter () method, Another call to Request.getinputstream () or Request.getreader () has not been able to get the content in the stream because the system may have read the data submitted in the form as a stream when calling Request.getparameter () Vice versa.
When the form form content is enctype=multipart/form-data encoded, call Request.getparameter () gets no data, even if the Request.getparameter () is already called Method can also get the data in the form by calling Request.getinputstream () or Request.getreader (), but Request.getinputstream () and Request.getreader () Cannot be mixed in the same response, if mixed use throws an exception.