Difference between get and post methods in Form

Source: Internet
Author: User

I. Question proposal
<Form action = "getpostservlet/getpost. do? Param4 = param4 "method =" get ">
<Input type = "hidden" name = "param1" value = "param1">
<Input type = "hidden" name = "param2" value = "param2">
<Input type = "text" name = "param3" value = "param3" readonly>
<Input type = "Submit" name = "button1" value = "Submit">
</Form>

Notice the action in the form: getpostservlet/getpost. do? Param4 = param4
This action has a parameter param4,
If the get method is used for submission, the backend cannot receive this parameter;
If the post method is used for submission, the backend can receive this parameter.
A brief explanation of the cause:
The URL submitted using the get method is shown as follows:
Http: // localhost/mywebapp/getpostservlet/getpost. do? Pram1 = param1 & pram2 = param2 & pram3 = param3 & button1 = submit that is to say, when the method is get, the parameter list behind the action itself will be ignored, and the backend cannot receive this parameter, only parameters in the form can be obtained;
The URL submitted using the POST method is shown as follows:
Http: // localhost/mywebapp/getpostservlet/getpost. do? Param4 = param4
That is to say, to submit a form in post mode, the parameters are divided into two parts: the parameters in the action are placed in the address bar, and the parameters in the form are placed in the Request Header; therefore, all data can be obtained in the background.
For the get method, the server uses request. querystring to obtain the value of the variable. For the POST method, the server uses request. Form to obtain the submitted data.

For the get method, the server uses request. querystring to obtain the value of the variable. For the POST method, the server uses request. Form to obtain the submitted data. The request reads data from several sets in order. The order from the beginning to the end is querystring, form, and servervariables. The request object searches for the variables in these sets in order of this order. If there is any matching variable, it will be aborted and the subsequent variables will be ignored.

1. Get and post Definitions

The method attribute of the form element is used to specify the HTTP Method for sending the form;

When get is used, the form dataset is appended to the URL specified by the Action attribute of the form element;

When post is used, the form dataset is encapsulated in the Request body and sent.

Use the get submission method to generate URL: User. do? Loginid = ABC

Get only concatenates a URL and then directly requests data from the service area. The dataset to be submitted to the server is included in the URL.

Use the post submission method to generate URL: User. Do

Post will wrap the form dataset, that is, the key-Value Pair loginid = ABC, in the Request body, send it to the server, and then request data from the server.

Ii. Differences between get and post

1. Security

If you use get to submit a form that verifies the user name and password, it is generally considered insecure because the user name and password will appear on the URL and then appear in the browser's historical records.

Obviously, post should be used when there are security requirements.

2. Encoding

Get can only send ASCII characters to the server, while post can send all the characters in ISO 10646.

The enctype attribute corresponding to get and post is different. Enctype has two values. The default value is application/X-WWW-form-urlencoded (encode all characters before sending)

The other is multipart/form-data (for file upload), which can only be used for post.

3. The length of the submitted data.

IE limits the length of the request URL to 2083 characters, thus limiting the length of data submitted by get. If the URL exceeds this limit, ie will not reflect any response when submitting the form.

4. Cache

Because a get result corresponds to a URL directly, the get result page may be cached by the browser, but post is generally not.

5. References and Seo
You can use a URI to reference A get result page, but the post result cannot be searched by the search engine.

 

Essential differences:

1,Get is used to obtain data from the server, while post is used to transmit data to the server.

If the post method is used, the browser will send data in the following two steps.

First, the browser establishes a contact with the form processing server specified in the Action attribute. Once a connection is established, the browser sends data to the server in a multipart transmission mode.

On the server side, once a post-style application starts to execute, it should read parameters from a flag location. Once the parameters are read, before the application can use these form values, these parameters must be decoded. the user-specific server will specify how the application should accept these parameters.

Use the get method:

The browser will directly establish a connection with the form processing server, then directly send all the form data in one transmission step, and then directly send all the form data in one transmission step.

2. Get adds the data in the form to the URL pointed to by action in the form of variable = value, and the two use "?" And each variable is connected by "&". Post puts the data in the form data body and passes the data to the URL indicated by the action according to the corresponding variables and values.

3. Correct Use of get and post
Use get only when form is idempotent.
Power Equality: the side effects of multiple identical requests are the same as those of one request.

If you submit a request to retrieve data from the server rather than performing other operations, and submit the request multiple times without obvious side effects, you should use get.
For example: Search Engine Query (http://www.google.com/search? Q = ABC) and paging (user. do? Page = 1)

If you submit this request, other operations and impacts will occur, you should use post.
For example, modify the data in the database on the server, send an email, and delete a file.

Another consideration is security.

If any of the following conditions is met, the POST method is used:
A. The request results have continuous side effects, such as adding new data rows to the database.
B. If the get method is used, the data collected on the form may make the URL too long.
C. The data to be transmitted is not encoded in ASCII format.

If any of the following conditions is met, use the get method:
A. The request is for searching resources. HTML form data is only used for searching.
B. The request results have no sustained side effects.
C. The total length of the collected data and input field names in the HTML form cannot exceed 1024 characters.

Iv. browser differences
IE6: the URL length is limited to 2083 characters. After the post operation, the page will not be refreshed and the data will not be automatically re-posted, and a warning will appear.
IE7 and IE6 are the same.
Firefox: If you refresh the page, the system will not automatically repost the data and a warning will appear.

 

The difference between post and get is easily overlooked:

When the method is get, the parameters following the URL in the Action attribute are ignored.

For example:

Action = insert. jsp? Name = Tober method = Get. After we submit the file, the actual URL does not contain name = Tober. It will be re-assembled into a URL Based on the content in the form, if the form contains a text box <input type = "text" name = "Address" value = "Wuhan">, the URL is changed to insert. JSP? Addresswuhan.

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.