The difference between get and post

Source: Internet
Author: User

Http://www.cnblogs.com/xrq730/p/4909607.html

The difference between the original and the rational

HTTP defines the different ways to interact with the server, with 4 basic methods, GET, POST, PUT, DELETE, respectively. URL full name is a resource descriptor, we can think: a URL address, which is used to describe a resource on the network, and HTTP GET, POST, PUT, delete corresponding to the resource of the search , change , 4 operations were added and deleted . So the first difference between get and post: Get is typically used to get/query resource information, and post is typically used to update resource information.

1, according to the HTTP specification, get for information acquisition, and should be safe and idempotent, security and idempotent means:

(1) So-called security means that the operation is used to obtain information rather than to modify the information, in other words, get requests should generally not produce side effects, that is, it is simply to obtain resource information, like a database query, does not modify, add data, does not affect the state of the resource

(2) So-called idempotent means that multiple requests to the same URL should return the same result

For example a news site, the reader opens a link to get the news should be implemented using get

2, according to the HTTP specification, post represents the possibility of modifying the resources on the server request, such as a news site, the reader's comments on the news resources should be implemented through post, because after the submission of the review site resources have been different, or that the resources have been modified

Another important point is to initiate an HTTP request from the browser:

1, some static resources such as. css files,. js files are rarely changed, so that these files in the browser cache, the request from the browser cache to take these resources without going to the server, can effectively reduce the pressure on the service side, speed up the browser access speed

2. The server may have some resources in the client's browser cache after the response.

only the Get method can save and fetch resources from the cache , and the Post method cannot.

Facts speak: The difference in appearances

Speaking of the difference between the original reason, we speak with facts, from the appearance of the difference between get and post, first I write a servlet:

PublicClass GetandpostservletExtendshttpservlet{/*** Serialization*/PrivateStaticFinalLong Serialversionuid = 1L;ProtectedvoidDoget (HttpServletRequest request, httpservletresponse response)throws Servletexception, IOException {System.out.println ("Enter getandpostservlet.doget ()"); String str = request.getparameter ("a"); System.out.println ("a =" + str);} protected void doPost (httpservletrequest request, httpservletresponse response) throws  Servletexception, IOException {System.out.println ("Enter getandpostservlet.dopost ()"); String str = request.getparameter ("a"); System.out.println ("a =" + str);}}            

The servlet configuration in Web. XML is not released, it will.

1, the URL request is the way to get . I enter "Http://localhost:8080/TestWeb/submitMethod" from the browser navigation bar to see the results of the printing:

Enter getandpostservlet.doget () a = null

2. Form default submission method is get mode . My JSP page is:

...
<body> <form action= "Submitmethod" > <input type= "Submit" value= "Submit"/> </form ></body>
...

The contents of the console print are:

Enter getandpostservlet.doget () a = null

3, the form of the action in the back with the parameters, only post mode can be taken. the JSP page does not change, let the form be submitted in get way first, run the result to:

Enter getandpostservlet.doget () a = null

If I change the form submission to post:

...
<body> <form action= "submitmethod?a=1" method= "POST" > <input type= "Submit" value= "Submit"/ > </form></body>
...

The result of spooling is:

Enter getandpostservlet.dopost () a = 1

4, form submission, the Get method will take all the control values behind the action, even if it is password, is also in clear form, the Post method will not . I modified the JSP:

...
<body> <form action= "submitmethod?a=1" method= "GET" > Name: <input type= "text" name= "name"/ ><br/> Password: <input type= "password" name= "password"/><br/> sex: <input type= " Radio "name=" Sex "value=" 0 "/> Male &nbsp;&nbsp;&nbsp; <input type= "Radio" name= "Sex" value= "1"/> female <br/> hobby: <input type= "checkbox" name= "hobby" value= "0"/> Play &nbsp;&nbsp;&nbsp <input type= "checkbox" name= "Hobby" value= "1"/> Singing <br/ > <input type= "Submit" value= "Submission"/> </form></body>
...

The form on the page is written like this:

Click Submit, the URL in the navigation bar is "http://localhost:8080/testweb/submitmethod?name=aaa&password=bbbccc&sex=0& Hobby=0&hobby=1", see even ciphertext password, inside the URL also became clear, the original" A=1 "does not appear in the URL.

Give the form to post, click Submit, the navigation bar inside the URL is "http://localhost:8080/TestWeb/submitMethod?a=1", action inside how or how.

From this point of view, it makes sense to say that the post submission form is more secure than the Get method to submit the form, because the data submitted by the Get method, the form parameters are in the URL, and the data submitted by the post, the form parameters are in the request body, can prove this point, F12 View the request information when the Post method is submitted:

Content-length is the request body, now is the 46,checkbox I only choose one time, Content-length becomes 35, and the Get method submits the data does not content-length this header information, It proves that the form parameters submitted by post are stored in the HTTP body.

The difference between get and post

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.