Garbled, see garbled again

Source: Internet
Author: User

I believe that all the people who write JSP have encountered garbled code. It seems that JSP garbled code is a very classic issue. In the past, when garbled characters were encountered, they were basically handled. At that time, there were only two types of garbled characters:

1. garbled Chinese characters on the page

I have encountered a small number of such problems, mostly because I did not add

<% @ Page contenttype = "text/html; charset = gb2312" %>, or the character set specified in the Code is inconsistent with that specified in <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">, this should be done well.

2. Read garbled data from the form

This is basically what I often encounter. It is usually like this name = request. getparameter ("name"), on the page

When the surface is printed out, It is garbled, this usually as long as the re-transcoding can be done, as shown in the following code: Name = new string (name. getbytes ("ISO-8859 -1" ), "GB 2312" );

I encountered a garbled problem the day before yesterday, so I couldn't solve the problem based on my previous experience.

On how the transcoding can not, the situation is like this: Because the user requires all the places are unified as a UTF-8 encoding, so the following command is used, <% @ page contenttype = "text/html; charset = UTF-8 "%> and <meta http-equiv =" Content-Type "content =" text/html; charset = UTF-8 ">,

When request. getparameter is used in the form to obtain the parameter, garbled characters are displayed. In this case, the request. setcharacterencoding ("UTF-8") is added. there is basically no garbled code. However, it won't work if I pass Chinese parameters through URLs, no matter how I transcode or modify the tomcat configuration file. My code is similar to this:

VaR name = "<% = c_name %> ";

Window. Location = URL + "? Name = "+ name +" & unit = "+ Unit

C_name is Chinese. I tried a lot of Methods online, but I can do it after the following:

VaR name = "<% = java.net. urlencoder. encode (c_name," UTF-8 ") %> ";

Window. Location = URL + "? Name = "+ name +" & unit = "+ unit.

Note: although the Garbled text problem is generally solved, I have been trying to find out the root cause of Garbled text. I have seen a post before and elaborated on the cause of Garbled text, unfortunately, it was too impetuous and I couldn't find it after reading it. If any brother knows it, let us know that this is the root solution to the problem, ^ _ ^.

 

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.