JSP's summary of Chinese garbled characters in URL links, jspurl

Source: Internet
Author: User

JSP's summary of Chinese garbled characters in URL links, jspurl

IE by default, the parameters following the URL are not encoded and sent, but Tomat is URL encoded by ISO8859-1 by default, so there is an error.

Method 1:

Perform secondary encoding on the URL link:

<A onclick = "javascript: window. open (encodeURI ('./DispatchAction. do? EfFormEname = FKRY0001 & code_type = Chinese parameter ') "> test </a>

Or encode the parameters separately:

Var code_type = "Chinese parameter"; code_type = encodeURI (code_type); window. open ("./DispatchAction. do? EfFormEname = FKRY0001 & code_type = "+ code_type); Service: String code_type = request. getParameter ("code_type"); // This statement must be written, because if it is not written, the encoding is % E5 % A6 % 88% code_type = java.net. URLDecoder. decode (code_type, "UTF-8 ");

Someone suggested why we need to recode the string twice on the client?

If the project requires, you cannot specify the encoding rules used by the container to decode submitted parameters. For example, if you want to receive parameters from different pages that are not encoded. (Or the developer is confused by this complicated thing, and does not know how to properly handle the parameter reception)

At this time, the client performs secondary encoding on the parameters, which effectively avoids the "Multi-byte character submission" problem.

Because of the first encoding, your parameter content does not contain multi-byte characters and becomes a pure Ascii string. (The result of the first compilation is called [STR_ENC1. [STR_ENC1] does not contain multi-byte characters)

After the re-encoding, submit, receive the container automatically resolved once (container automatically resolved this time, whether by GBK or UTF-8 or ISO-8859-1 is good, can be correctly obtained [STR_ENC1])

Then, implement decodeURIComponent in the Program (Java usually uses java.net. URLDecoder. decode (***, "UTF-8") to get the original value of the parameter you want to submit.

Simply put, the Tomcat server will automatically perform URLDecode for you once, and add the URLDecode you wrote in the Service Code. There are two decodes in total. Since Decode is required twice, Encode is required twice. Maybe you will ask, simply Encode once, and then do not Decode in the java code. Haha, this is also not acceptable. This is actually why two encodes are required.

Method 2: (IE8 is not supported by tests)

Http://xxx.do? Ptname = Chinese parameter String strPtname = request. getParameter ("ptname"); strPtname = new String (strPtname. getBytes ("ISO-8859-1"), "UTF-8 ");

Method 3:

<% @ Page contentType = "text/html; charset = gb2312" %> <a href = "ds. jsp? Url = <% = java.net. URLEncoder. encode ("encode here", "GB2312") %> "> click here </a> <% // request. setCharacterEncoding ("GBK"); if (request. getParameter ("url ")! = Null) {str = request. getParameter ("url"); str = java.net. URLDecoder. decode (str, "GB2312"); str = new String (str. getBytes ("ISO-8859-1"); out. print (str) ;}%>

Method 4:
In Tomcat, set Connector in server. xml to familiarize yourself with URIEncoding = "UTF-8" to ensure that the decoding format is consistent with the encoding format.

Articles you may be interested in:
  • How to transmit Chinese parameters through URLs in jsp
  • How to obtain url parameters on the jsp page
  • Passing and parsing URL parameters and Chinese transcoding and Decoding in JSPs
  • Javascript uses url to pass Chinese parameters to jsp pages, resulting in garbled code Solution

Related Article

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.