Get and post in the Web

Source: Internet
Author: User

The client submits data to the server side in two ways get and post,get is to stitch the data to the URL, and post is to encapsulate the data in the request body and send the past. As the name implies, get is a request for data, sometimes it needs to be accompanied by some parameters, post is sent data, so need to carry data.

First, get mode

  Get requests are secure, idempotent, and so on.

1, security: The get operation does not modify the server's data, no matter how many get requests, the server's data will not change.

2, Idempotent: Idempotent is said, the same request is sent unchanged N and M times, the state of the resources on the server is ultimately consistent, the corresponding server returned content is consistent.

 A GET request sends a small amount of data.

1, the HTTP protocol get/post does not send data size restrictions, the size of the sending data is the browser and the operating system, server,HTTP itself does not limit the length of the URL .

The URL is long, and server processing is a burden. The original session will not have much data, if the malicious construction of a few m-size URLs, and non-stop access to your server, the number of server concurrency will drop.

 Get can be cached, post cannot be cached

Open a page, if it was opened before, it is obvious that the opening speed will be accelerated, this is because html/js/css/img and other files can be cached by the browser, and these files are obtained by using a GET request. I've only seen the POST request so far in Ajax and form forms.

Second, POST mode

  The POST request is secure

Get is stitching data onto a URL, and post is encapsulating the data in the request body. The parameters attached to a GET request can be seen directly on the browser's address bar, or viewed in the browser's history or log. The post is not cached and cannot be saved as a bookmark, so the request is not logged? The request parameters cannot be intercepted? No, grab a bag and you can see it.

Therefore, the POST request is only relatively secure.

Third, GET request garbled

(1) The data in the GET request is spliced directly on the URL, using & separating key-value pairs. But sometimes key,value will appear Chinese and other characters that are unsafe for HTML standards
(2) The HTML standard says, In addition to the characters "a"-"Z", "a"-"Z", "0″-" 9″, ".", "-", "*", and "_" the other characters are unsafe and need to be encoded. The "" Space is encoded as a + sign. when unsafe characters are present, the browser encodes these parameter values before being sent to the server.

(3) The general recommendation is to use the UTF-8 encoding format. You can also use JavaScript to encodeuricomponent the data (URL);

  Causes of garbled characters

now the URL is the ASCII range of characters, and then converted to iso-8859-1 encoding to binary with the request header sent out, for the Get method, there is no request entity, the URL containing the data in the request header. Please note that this is actually two times encoded, the first time is to use UTF8, the second time using ISO-8859-1 encoding to be able to transmit binary 101010 on the network .... Now the problem comes to the server side, each server default encoding may be different, such as the Tomcat default encoding is iso-8859-1. according to the logic of the server will also do two times the decoding action, the first is the binary content of the iso-8859-1 decoding, the second is to use the server default encoding to decode the data,so the data we get using Request.getparameter ("name") is decoded two times.
when Tomcat uses Iso-8859-1 to decode the data for a second time, because the corresponding client-side encoding is UTF8,so we use Request.getparameter ("name") is definitely garbled.If we don't change Tomcat's default encoding, you can use theNew String (Request.getparameter ("name"). GetBytes ("Iso-8859-1″)," Utf-8″), and manually re-decode.
request.setcharacterencoding ("Utf-8″") this way is not valid for the GET mode commit data,but submitting data to post is valid. Because get has no request body.

Solution Solutions

1. The usual practice is to modify the Tomcat's default encoding: in the Server.xml connector plus uriencoding= "Utf-8″ can

2.post mode
the data submitted by post must also be encoded. if the HTML file where the form resides specifies an encoding, the encoding is used for URL encoding.

In order to prevent garbled, general system-related files are set to UTF8 format, Web server, Java Server, database encoding format is set to UTF8. This is generally less garbled than the problem.
There is also the use of post as far as possible to submit data, one is because the length of the URL is limited, and the Get method is to stitch the data on the URL.

Get and post in the Web

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.