Page use href to background parameters with Chinese background gets the garbled characters

Source: Internet
Author: User

Meet this problem when the brain is a lump of paste, usually do not pay attention to this detail, the first reaction is Baidu, but tried some methods and can not solve the problem, eventually feel that they understand too shallow, continue to try to go on no meaning.

Search the Internet for the definition of various coding formats, for the simple mind of me, these definitions are too complex. Summing up, at present we generally do development with the UTF-8 format, which belongs to the Unicode encoding. To avoid garbled behavior, you must note that the format is consistent from the foreground to the backstage to the foreground.

The problem I encountered was this:

Front page file encoding format is UTF-8, that is, <meta charset= "UTF-8", and then the page has a place to use the href to the backstage, the href inside with the Chinese parameters, to the backstage found these Chinese garbled, I thought as long as I eclipse set encoding format is UTF-8 with the foreground is not a problem, but in fact, the format of the href is not UTF-8, should be just the content of the page is really UTF-8, but the href should be related to the browser, and not necessarily this format, So the backstage after the garbled. There are three ways to solve this problem:

1. The simplest way is to change the Tomcat server.xml file, add uriencoding= "UTF-8" This means to set the data transmission time encoding, but the background still have to Urldecoder.decode (parameters, "Utf-8") to do processing.

2. Online also useful encodeURIComponent method, tried a bit, is also possible, the principle is the same, in the front desk will be Chinese first through encodeURIComponent (encodeURIComponent (Chinese parameters)) Two times the Chinese encoding, backstage with Urldecoder.decode (Chinese parameters, "UTF-8") decoding, and eventually converted to UTF-8 in the background.

3. In the foreground, the Chinese is encoded encodeuricomponent (Chinese parameter), the background string str=new string (Request.getparameter ("Chinese parameter"). GetBytes (" Iso8859-1 ")," UTF-8 ") gets.

Then tried again, regardless of the foreground or two times encoding, the background with string Str=new string (Request.getparameter ("Chinese parameter"). GetBytes ("Iso8859-1"), "UTF-8") The fetch can be successful, but if you use Urldecoder.decode () in the background, the foreground must be encoded two times.

Page use href to background parameters with Chinese background gets the garbled characters

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: 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.