Difference between get and post and urlencode and garbled characters

Source: Internet
Author: User
Tags form post

Project can not avoid a place to pass Chinese, the previous encounter garbled code is first in JS (coding all set on the page for the UTF-8)

JsonStr = encodeURIComponent (jsonStr ));

In the JAVA code

ProjName = URLDecoder. decode (projName, "UTF-8 ");

However, this is a good case for a small number of fields. If there are many fields, it may not be very good. This time I met it and found it online, we can see the following introduction to the encoding method when the get and post methods are submitted. I tried it and it was useful. Let's share it with you.

Original article: http://blog.sina.com.cn/s/blog_697bab7b0100r8bt.html

 

In form 2, you can submit the data to the server, get and post, respectively.
(1) get submit
 1. First, let's talk about how the form on the client (browser) uses the get method to encode the data and submit it to the server.
 
   The get method concatenates data after the request url as a parameter, for example, http: // localhost: 8080/servlet? Msg = abc
(A common garbled problem occurs. If a url contains Chinese or other special characters, for example, http: // localhost: 8080 // servlet? Msg = Hangzhou, the server can easily get garbled characters). After url splicing is complete, the browser performs urlEncodeAnd then send it to the server, URLEncodeThe process is to encode part of the url as a character into a binary byte code according to a certain encoding method (such as UTF-8, gbk, etc.), and then each byte uses a word containing three characters
Symbol string "% xy", where xy is the two hexadecimal representation of the byte. I may not be clear about this. For more information, see the introduction of the java.net. URLEncoder class. Learn about URLEncodeThe process, we can see two very important issues, first: the need for URLEncodeGenerally, non-ASCII characters (generally speaking) are used as URLs for all texts except English letters (such as Chinese and Japanese ).EncodeTherefore, for us, URLs with English letters will not cause server garbled characters. garbled characters are caused by Chinese or special characters in URLs. Second: urlEncodeWhich encoding method is used for character encoding? In this case, different browsers have different practices. The Chinese version of browsers generally use GBK by default.
The browser can also use UTF-8, may be different users have different browser settings, it also causes different encoding methods, therefore, many websites use the Chinese or special characters in the url as URLs first.EncodeAnd then splice the url to submit the data, that is, the URL for the browserEncodeThe advantage is that the website can submit data encoding in a unified get method.
Completed URLEncode, Then the current url becomes characters in the ASCII range, and then converted to binary in iso-8859-1 encoding mode with the request header sent together. Here I want to say a few more words: For the get method, there is no request entity, and the URLs containing data are in the request header.EncodeIn my opinion, the reason is: For the request header, it is to use the iso-8859-1 encoding method to encode the binary 101010 ...... the pure data uploaded on the Internet
Send, If you directly contain Chinese and other special characters for iso-8859-1 encoding will lose information, so first URLEncodeIs necessary.
  2. Server Side (Tomcat.
  The first step is to first decode the data with a iso-8859-1, for the get method,TomcatThe request url contains parameter data. If the request URL contains special characters such as Chinese charactersEncodeAfter % XY status, stop first. Let's talk about the process where developers generally obtain data. Generally, requests are obtained by request. getParameter ("name ").
When we get the parameter data, the request object or the data is decoded, and cannot be specified in the program during the decoding process. Here, there are many new users who use the request. setCharacterEncoding ("Character Set") can specify the decoding method. In fact, it is not feasible. Refer to the official API description of servlet to explain 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 (). We can see that the get method is powerless. So what encoding method is used to decode the data? This isTomcatThe thing, the default is the use of the iso-8859-1, so we can find out why the get request with Chinese parameters in the server side to get garbled, the reason is that the client generally uses a UTF-8 or GBK for the Data URLEncode, URL in iso-8859-1 Mode
The decoder obviously does not work. In the program, we can directly
Java code
1. New String (request. getparameter ("name"). getbytes ("iso-8859-1"), "URL specified by the clientEncodeEncoding method ")
Restore the bytecode and then decode the data in the correct way.TomcatConfigure
XML Code
1. <connector Port = "8080" protocol = "HTTP/1.1" maxthreads = "150" connectiontimeout = "20000" redirectport = "8443" uriencoding = "GBK"/>
In this wayTomcatAfter obtaining data, use the specified URL decoder. The URL decoder is described here.

(2) Post submission
1. The Form of the client (browser) uses the POST method to encode the data and submit it to the server.
 The data to be transmitted in the POST method also needs URLEncodeSo what encoding method does he use?
  In the HTML file where the form is located, if there are <meta http-equiv = "Content-Type" content = "text/html; charset = character set (GBK, UTF-8, etc) "/>, the post will be encoded using the encoding method specified here. Generally, we think that this code is used to let the browser know which character set is used to interpret the webpage. Therefore, the website will put it at the front end of the HTML code and try not to see garbled characters, in fact, it also serves to specify the URL for the Form POST method to submit data.EncodeEncoding method. From this we can see that for the get method, the browser's URL to the dataEncodeThe encoding method is determined by browser settings (you can use js for unified designation), while the post method can be specified by developers.
2. Server Side (Tomcat.
If you useTomcatThe default settings, no encoding settings such as the filter, then he is also decoded with the iso-8859-1, but request. setCharacterEncoding ("Character Set") can come in handy.

I found thatTomcatThe premise is that the encoding method is not specified in the request header. If the encoding method is specified in the request header, the encoding method is used.

Two articles are recommended. The addresses are
URL encoding: http://www.cnblogs.com/yencain/articles/1336686.html;
The form uses the post method to submit data garbled problem: http://wanghuan8086.javaeye.com/blog/173869

If there is a <meta http-equiv = "Content-Type" content = "text/html; charset = character set (GBK, UTF-8) "/>
Post submission is strongly recommended.

Attach URLEncodeAnd UrlDecode JavaScript function code and, character encoding method introduction, http://hi.baidu.com/loongdao/blog/item/6c39ec863280bd3666096ef6. Html

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.