Several solutions for solving Chinese garbled characters (recommended) _jsp programming

Source: Internet
Author: User
Tags character set form post set set tomcat

First of all, explain my special situation:

1. Foreground JSP, I am using form POST request, set the Enctype= "Multipart/form-data", the page encoding format is Utf-8

2. In the background, I am using the Commons-fileupload component, Servletfileupload parsing form forms and files,

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

4. Set up servletfileupload. Setheaderencoding ("UTF-8");

5.TOMCAT configuration below Server.xml has also been set up uriencoding= "UTF-8";

At this point, according to all the formats are matched, before and after the corresponding, parse out the affirmation is utf-8, but after FormField parse out is still iso-8859-1 format coding,

Enctype= "Multipart/form-data" will pass the data in a 2-encoded format, so I'm sure there was a problem with servletfileupload parsing, multiple lookups,

My question is missing a step string formfieldvalue = Fileitem.getstring ("UTF-8");

Six Chinese garbled processing methods for JSP and servlet

One, the form submits the garbled appearance:

In the form submission, often submitted some Chinese, naturally will not be able to avoid the occurrence of Chinese garbled, for the form has two ways to submit: Get and post submission. So there is a GET request and a POST request when the request is requested. Each method has a different solution, the reason is garbled, because the GET request, the data passed to the server is appended to the URL address, and the post request, the data passed to the server as part of the request is passed to the server. This leads to a different way of handling the garbled characters they produce.

1, the client's GET request

When a get commits, the container encodes the container encoding if the Tomcat default encoding is iso-8859-1 the server.xml inside the code or

The following code is like

String name = Request.getpara ... ("name"); 
String strName = new String (Name.getbyte ("iso-8859-1"), "GBK");

For different ways of requesting, solve the garbled problem is not the same, for the client GET request, server-side processing to want to not appear garbled, solve this problem slightly more complex, need to use a string type of constructor, one of the constructors is the specified encoding to decode, generally used "UTF-8" way. As long as the requested parameter is reconstructed into a string on the server side.

After construction, the client enters Chinese and, in the case of a GET request in the form, Str becomes Chinese.

2, the client's post request

For the client post request, the problem of handling garbled is simpler because the requested data is passed to the server as part of the request, so just modify the code within the request. As long as the requested data is set to "UTF-8" at the very beginning of the server side, enter the following statement: request. Setcharacterencoding ("UTF-8") so that the user in the server-side to get the Chinese data is no longer garbled.

Second, when the hyperlink appears garbled (low version browser not IE6)

In web development, a lot of times are through hyperlinks to pass Chinese parameters, this will also result in the display when the garbled, for the hyperlink, it is actually sent to the server side of a request, and it sent a request belongs to get request, so for the hyperlink garbled, It handles garbled ways and forms of get requests appear garbled way is the same.

Third, the redirection occurs garbled (Low version browser not IE6)

Sometimes write response Sendredirect method for redirection will also appear garbled, redirect is actually sent a request to the server, so the method of solving garbled and the above is the same.

Four, the browser version of the low resulting garbled

When surfing the internet, sometimes some information submitted in the Address bar shows the words "%2c%c6%cc%c6", in fact, this is to prevent the emergence of garbled solution, if your browser is IE6 or the following version, then our second case and the third situation will appear garbled (especially when the Chinese is odd That's not going to make it so we have to adopt a more practical approach:

The Urlencoder class and Urldcoder classes are provided in the java.net package, which provides encode and decode two static methods, respectively, for encoding and decoding. We will be passing the Chinese parameters to encode, after passing to the server, the server decoded, you can display Chinese.

To encode: Urlencoder.encode (stuname, "UTF-8")

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

To decode: Urldecoder.decode (Stuname, "UTF-8")

Five, return to the browser display garbled

In servlet programming, it is often necessary to return some information to the browser through the response object to our clients, while we display the Chinese on the server side, but the response to the client browser is garbled, mainly due to the getwriter of the Response object () The PrintWriter object returned by default uses the "Iso-8859-1" character set encoding to convert a Unicode string to a byte array, because the iso8859-1 character set does not contain Chinese characters at all, So Java in the conversion will be invalid character encoding output to the client, so there will be garbled, for this servletresponse interface defines the setcharacterencoding, setContentType, and so on, to specify the character set encoding used by the PrintWriter object that the Getwriter method returns, so we set the values of these methods in the Write Servlet program before calling the Getwriter method.

As long as you write a servlet file that contains information that responds to the client, write these two words. It is best to write a second sentence because it has a high priority, and its setting results will overwrite the character encoding set set by methods such as setContentType.

Vi. Modifying Tomcat encoding

There is a solution to the garbled problem caused by get requests, we often use Tomcat as a container to run the servlet and JSP, and Tomcat's internal default encoding is iso-8859-1, so the data (URI) passed to the GET Request method is appended to the resource being accessed, its encoding is tomcat default, and if the encoding of the URI is modified, there will be no garbled code including the above mentioned redirects and hyperlinks for all GET request methods. Find where to modify Tomcat's port in the Tomcat profile Server.xml, and add the uriencoding attribute inside it, set to the same value as the encoding you set in your project, all of which are UTF-8.

In the preparation of servlet and JSP, in order to avoid garbled, the most important thing is: the use of consistent coding, if the code is consistent, will not appear garbled.

The above solution of the Chinese garbled several solutions (recommended) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.