Current location - Loan Platform Complete Network - Big data management - Difference and use of get and post in html
Difference and use of get and post in html
1. Get is used to get data from the server, while Post is used to pass data to the server.

2, Get will be in accordance with the form of data in the form of variable=value, added to the action of the URL pointed to, and the two use "?" connection, and the use of "&" connection between the variables; Post is the form of the data in the form of the data body, in accordance with the variables and the value of the corresponding way, passed to the action pointed to the URL.

3, Get is not safe, because in the transmission process, the data is put in the request URL, and many servers, proxies, or user agents existing today record the request URL in a log file and put it somewhere so that some private information may be seen by a third party. In addition, the user can also see the submitted data directly in the browser, and some internal system messages will be displayed to the user together. all the operations of Post are invisible to the user.

4. Get transfers a small amount of data, which is mainly due to the limitations of the length of the URL; and Post can transfer a large amount of data, so in the upload file can only use Post (of course, there is another reason, will be mentioned later).

5, Get restricts the Form form of the data set of values must be ASCII characters; and Post supports the entire ISO10646 character set. The default is to encode in ISO-8859-1

6, Get is the default method of Form.

The following comparison is very very use:

Doing java web development for some time, there is a problem is always bothering me, that is, the problem of garbled code, basically online to find a solution (online information is really a lot of), are a whole lot of introduction to how to solve this kind of garbled problem, but there are not a few of the problem of the ins and outs of the said clearly, sometimes read some of the articles After reading some articles, I thought I understood, but in the development of garbled problems like ghosts come out to scare people, really a big head! This article is a long time and messy code to do the struggle of some of the accumulation of understanding, but also hope that there are more friends to give guidance and add.

form has 2 methods to submit data to the server, get and post, respectively.

(a) get submit

1. First of all, the client (browser) form with the get method is how to encode the data and submit it to the server.

For the get method, are the data strings in the request url as a parameter, such as: . URLEncoder class is introduced here. Understand the process of URL encode, we can see 2 very important issues, first: the need to URL encode the characters are generally non-ASCII characters (in general terms), and then in general terms is in addition to the letters of the alphabet other than text (such as: Chinese, Japanese, etc.) are to be URL encode, so for us, are the letters of the alphabet of the url does not server to get the problem of garbled code, garbled is the url with Chinese or special characters caused by; Second: URL encode in accordance with that kind of encoding in the end of the character encoding? Here is the matter of the browser, and different browsers have different practices, the Chinese version of the browser will generally use GBK by default, through the settings browser can also use UTF-8, may be different users have different browser settings, which also results in a different encoding, so the practice of many Web sites are the first url inside the Chinese language or special characters using the javascript to do URL encode, and then splice the url to submit data, that is, for the browser to do the URL encode, the benefit is that the site can be unified get method to submit data encoding. Completed the URL encode, so now the url has become an ASCII range of characters, and then converted to binary with the request header with the iso-8859-1 encoding sent out. Here I would like to say a few more words is that, for get method, there is no request entity, the url containing data are in the request header, the reason why I use URL encode, I personally feel that the reason is: for the request header is ultimately encoded in iso-8859-1 encoding into binary 101010..... If the request header is ultimately encoded as binary 101010 pure data on the Internet, if it contains special characters such as Chinese, it will lose the information, so it is necessary to do URL encode first.

2. server-side (tomcat) is how to get the data to decode.

The first step is to first decode the data with iso-8859-1, for the get method, tomcat to get the data is the ASCII range of request header characters, which request url with parameter data, if the parameters have Chinese and other special characters, then the current or URL encode after the %XY state, stop, we first say that the developers generally get the data process. developers generally get the process of data. Usually we are request.getParameter ("name") to get the parameter data, we are in the request object or get the data are decoded, and the decoding process can not be specified in the program, it should be said here, there are a lot of newbies say with request.setCharacterEncoding ("character set") can specify the decoding method, in fact, it is not possible to look at the servlet's official API description of the explanation of this method: Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). As you can see there is nothing he can do about the get method. So in the end with what encoding decoding data, this is tomcat things, the default default iso-8859-1, so we can find why get request with Chinese parameters why the server side to get the code, the reason is that in the client side are generally used UTF-8 or GBK on the data URL encode, here iso-8859-1 way URL decoder is obviously used to read the request parameters or reading input using getReader(). 1 way URL decoder obviously does not work, in the program we can directly

Java code

1. new String(request.getParameter("name").getBytes("iso-8859-1"), "Client-specified URL encode encode")

Restore back to byte code, then decode the data in the right way, online articles usually do a configuration inside tomcat

Xml code

1. <Connector port="8080" protocol="HTTP/1.1" maxThreads=" 150" connectionTimeout="20000" redirectPort="8443" URIEncoding="GBK"/>

This is to allow tomcat to get the data in the specified way after the URL decoder, URL decoder is introduced here

(a) post submit

1. How the client (browser) form form with the post method is to encode the data and submit it to the server.

The data to be transmitted in the post method should also be URL encoded, so what encoding method does he use?

In the html file where the form is located, if there is a section <meta http-equiv="Content-Type" content="text/html; charset=character set (GBK, utf-8, etc.)"/>, then post will be encoded with the encoding specified here. Generally we all think that this code is to let the browser know what character set to interpret the web page, so the site will put it in the html code at the forefront, as far as possible not to appear in the garbled code, in fact, it also has a role is to specify the form form post method to submit the data of the URL encode encoding method. From here we can see that for the get method to count, the browser on the data of the URL encode encoding method is to have the browser settings to decide, (you can use js to do unified specification), while the post method, the developer can specify.

2. How the server side (tomcat) gets the data and decodes it.

If you use tomcat default default settings, and did not do filters and other encoding settings, then he is also decoded with iso-8859-1, but request.setCharacterEncoding("character set") can come in handy.