Chinese garbled characters in URL Transmission

Source: Internet
Author: User
When passing the parameter value through the link is Chinese, there will be 2 problems: (the page uses the UTF-8 format) 1 "Chinese garbled, in the action when the value of transcoding can be (use new string (Str. getbytes ("iso-8859-1"), "UTF-8") 2 "passed the Chinese parameters followed by other parameters, at this time page:
Query where: The query condition search_name = "Zhang San", followed by npageno = 1; when the action is followed by a value, request. the value obtained by getparameter ("search_name") is: "James's garbled code" npageno = 1. After reading many posts on the internet, I tried it myself and changed it to the following: page: (do not use el language, use JSP scripts) <% string search_name = request. getparameter ("search_name"); If (search_name! = NULL) {// encode Chinese characters search_name = urlencoder. encode (search_name, "UTF-8") ;}%> query action followed by value: String search_name = This. getrequest (). getparameter ("search_name"); If (search_name! = NULL) {search_name = new string (search_name.getbytes ("iso-8859-1"), "UTF-8 "); }Analysis of the Reasons: Tomcat default encoding format iso-8859-1, request value encoding format changed, because the Chinese variable garbled, so the & npageno = 1 also got; so in the page to transfer the Chinese parameters to the UTF-8 format, take the action in the iso-8859-1 format of the Chinese and then converted to the UTF-8 format, the problem of Chinese solved. file 1: encode. JSP <% @ page Language = "Java" C pageencoding = "GBK" %> <% string sfilename = "Chinese file name .doc"; sfilename = java.net. urlencoder. encode (sfilename); // code line 1%> Decode File 2: Decode. JSP <% @ page Language = "Java" C pageencoding = "GBK" %> <% string sfilename = request. getpar Ameter ("FILENAME"); sfilename = java.net. urldecoder. decode (sfilename, "GBK"); // code line 3 out. println ("filename:" + sfilename); %> it looks like this can certainly meet the requirements, but this is not the case. JavaScript does not recognize the JSP encoded string: when you move the cursor over the link, you can see that the IE status bar is indeed correctly encoded, that is, "too many characters are encoded too many characters. Doc". However, note that: right-click the link and choose>. The URL displayed in the link is javascript: Decode ('? D ????? T ??. Doc '); The consequences can be imagined. garbled characters are output after turning. Get for a long time, turn UTF-8 is also the same, a small problem is very depressed, finally think of, have to start in JS here. Encode. comment out line 1 of JSP code, and add a line before line 2: filename = encodeuri (filename); see a little progress: the URL after turning has been very standard: filename.pdf Chinese file name .doc, but the result is still garbled. Why? Note that the preceding encoding is different from the following code! However, the Javascript encodeuri does not provide the encoding parameter, the silence is the UTF-8. Good, then sfilename = java.net. urldecoder. Decode (sfilename, "UTF-8"); line does not work? The answer is: No. I haven't found the reason. I will add it here if I find it. If anyone knows it, I hope you can give me some advice. Solution: replace line 3 with sfilename = new string (sfilename. getbytes ("ISO-8859-1"), "UTF-8"); now let's look at the results, it's already normal Chinese. Supplement: In Ajax, after the servlet outputs Chinese characters, it needs to be read by JS and faces encoding problems. You can use the following method: encode stext = urlencoder with urlencoder in servlet. encode (stext, "UTF-8"); Use decodeuri in JS to decode xtext = decodeuri (xtext );

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.