Solutions for Chinese Garbled text (recommended) and Chinese Garbled text

Source: Internet
Author: User
Tags form post

Solutions for Chinese Garbled text (recommended) and Chinese Garbled text

First, describe my special circumstances:

1. In front-end jsp, I used form post requests and set enctype = "multipart/form-data". The page encoding formats are UTF-8.

2. In the background, I am using the commons-fileUpload component. ServletFileUpload parses form forms and files,

3. Set request. setCharacterEncoding ("UTF-8 ");

4. Set ServletFileUpload. setHeaderEncoding ("UTF-8 ");

5. Tomcat configuration below server. xml has also set URIEncoding = "UTF-8 ";

At this point, according to the principle of all the formats are matched, before and after the corresponding, the parsed must be UTF-8, but after formfield resolution after any of the ISO-8859-1 format encoding,

Enctype = "multipart/form-data" will pass the data in a binary encoding format. Therefore, I decided that a problem occurred during the parsing of ServletFileUpload,

My problem missing a step String formFieldValue = fileItem. getString ("UTF-8 ");

Six Methods of garbled Chinese Characters in JSP and Servlet

1. garbled characters appear during form submission:

When submitting a form, you often submit some Chinese characters, which naturally prevents Chinese garbled characters. For a form, there are two submission methods: get and post. Therefore, there are get requests and post requests. Each method has a different solution. garbled characters occur because the data transmitted to the server after the get request is appended to the URL address, the data transmitted to the server is transmitted to the server as part of the Request body. This leads to different processing methods for Garbled text.

1. get requests from the client

When a get request is submitted, the container uses the container encoding. If the default tomcat encoding is used, the iso-8859-1 sets the encoding in server. xml or

The following code is as follows:

String name = request.getPara...("name"); String strName = new String(name.getByte("iso-8859-1"),"GBK");

For different request methods, the problem of solving Garbled text is also different. For client get requests, if the server side does not need Garbled text for processing, it is a little complicated to solve this problem, it needs to use the String Type constructor, one of the constructor is to use the specified encoding method to decode, generally use the "UTF-8" method. You only need to reconstruct the request parameters into a string on the server side.

After the construction, the client enters Chinese characters and the str becomes Chinese when the get request is made during the form.

2. Client post request

For post requests on the client, it is easier to handle Garbled text because the request data is transmitted to the server as part of the Request body, therefore, you only need to modify the encoding in the request. As long as the request data is set to "UTF-8" at the beginning of the server, enter the following statement: request. setCharacterEncoding ("UTF-8"); so that the user's Chinese data on the server is no longer garbled.

2. garbled characters appear during hyperlinks (IE6 is unavailable for browsers of earlier versions)

In Web development, Chinese parameters are transmitted through hyperlinks many times, which may cause garbled Characters During display. For hyperlinks, it actually sends a request to the server, and the request is a get request, so for the garbled URL, the method for processing Garbled text is the same as that for form get requests.

3. garbled characters occur during redirection (IE6 is not supported in earlier browsers)

Sometimes garbled characters may occur when the sendRedirect method of response is written for redirection. During redirection, a request is actually sent to the server, so the solution to garbled code is the same as above.

4. garbled characters caused by low browser versions

When surfing the Internet, some information submitted is displayed in the address bar "% 2C % C6 % CC % C6". In fact, this is a solution to prevent gibberish, if your browser is IE6 or earlier, garbled characters (especially when Chinese characters are odd) will occur in the second and third cases ), therefore, we must adopt another practical approach:

The URLEncoder class and URLDcoder class are provided in the java.net package. These two classes provide two static methods: encode and decode for encoding and decoding respectively. After the Chinese parameters to be passed are encoded, the Chinese parameters can be displayed after being passed to the server and decoded by the server.

Encoded: URLEncoder. encode (stuname, "UTF-8 ")

Pass to server: <a href = "/1.jsp? Stuname <% = stuname %> "> pass </a>

Decoding: URLDecoder. decode (stuname, "UTF-8 ")

5. Return the garbled characters displayed in the browser

In Servlet programming, it is often necessary to return some information to the browser through the response object and give it to our client. However, we display Chinese characters on the server side, but the response to the client browser is garbled, this is mainly because the PrintWriter object returned by the getWriter () method of the response object uses the "ISO-8859-1" character set encoding to convert Unicode strings to byte arrays by default, because the ISO8859-1 Character Set root does not contain Chinese characters, therefore, Java will output invalid character codes to the client during conversion, so garbled characters appear, therefore, the ServletResponse interface defines setCharacterEncoding, setContentType, and other methods to specify the character set encoding used by the PrintWriter object returned by the getWriter method. Therefore, in the Servlet program, set the value of these methods before calling the getWriter method.

Write these two sentences as long as the Servlet file contains the response information to the client. It is best to write the second sentence, because it has a high priority, and its setting result will overwrite the character delimiter set by setContentType and other methods.

6. Modify Tomcat Encoding

In the get request caused by garbled problem, there is a solution, we often use Tomcat as a container to run Servlet and JSP, and Tomcat internal default encoding is ISO-8859-1, therefore, for the get request method, the transmitted data (URI) will be appended to the accessed resource, and its encoding is Tomcat's default. If you modify the URI encoding, so there will be no garbled characters for all get request methods, including the redirection and hyperlink mentioned above, in the Tomcat configuration file server. in xml, locate the location where Tomcat's port is modified, add the URIEncoding attribute inside it, set to the same value as the encoding set in your project, all here are UTF-8.

When compiling Servlet and JSP, to avoid garbled characters, the most important thing is to adopt consistent encoding. If the encoding is consistent, no garbled characters will occur.

The above several solutions to Chinese Garbled text (recommended) are all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.

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.