The form form in HTML has a key attribute Enctype

Source: Internet
Author: User

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
  1. <span style="Font-size:small;" >post/post_test.php http/1.1
  2. Accept-language:zh-cn
  3. user-agent:mozilla/4.0
  4. content-type:application/x-www-form-urlencoded
  5. host:192.168.12.102
  6. Content-length:42
  7. Connection:keep-alive
  8. Cache-control:no-cache
  9. title=test&content=%b3%ac%bc%b6%c5%ae%c9%fa&submit=post+article
  10. </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
  1. <span style="Font-size:small;" >post/post_test.php? t=1 http/1.1
  2. Accept-language:zh-cn
  3. user-agent:mozilla/4.0
  4. Content-type:multipart/form-data; boundary=---------------------------7dbf514701e8
  5. Accept-encoding:gzip, deflate
  6. host:192.168.12.102
  7. content-length:345
  8. Connection:keep-alive
  9. Cache-control:no-cache
  10. -----------------------------7dbf514701e8
  11. Content-disposition:form-data; name="title"
  12. Test
  13. -----------------------------7dbf514701e8
  14. Content-disposition:form-data; name="content"
  15. ....
  16. -----------------------------7dbf514701e8
  17. Content-disposition:form-data; name="Submit"
  18. Post article
  19. -----------------------------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.

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.