Java garbled Solution

Source: Internet
Author: User

The jsp page encoding method needs to be set in two places:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<% @ Page contentType = "text/html; charset = UTF-8" %>

Here, pageEncoding refers to the encoding method when the jsp file is stored locally. The charset of contentType refers to the encoding used when the server sends the webpage content to the client.

From the first visit to a jsp page and the page is sent to the client, the Jsp page must undergo three encoding conversions:

The first stage is jsp compilation. java, it will read jsp according to pageEncoding settings, the result is translated by the specified encoding scheme into a unified UTF-8 JAVA source code (that is. java). If pageEncoding is set incorrectly or is not set, Chinese garbled characters are displayed.

The second stage is the JAVA source code of JAVAC to the compilation of java byteCode, No matter what encoding scheme is used in JSP writing, after this stage the results are all the java source code of the UTF-8 encoding.

JAVAC uses the UTF-8's encoding to read the java source code and compile it into the UTF-8's encoding binary code (that is,. class), which is the JVM's specification for the constant string expression in the binary code (java encoding.

The third stage is the JAVA binary code loaded and executed by Tomcat (or its application iner INER) in Stage 2. The output result is displayed on the client, in this case, the parameter contentType hidden in phase 1 and phase 2 is effective.

The final solution is:

Set one of pageEncoding or contentType on the jsp page to support Chinese encoding formats (such as UTF-8, gbk, and gb2312 ). If you set one, the other will be the same as it by default.

If both are set, make sure that both support Chinese encoding (not necessarily the same ).

The Recommended settings are as follows:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<% @ Page contentType = "text/html; charset = UTF-8" %>

2. garbled values transmitted in post mode:

Because the post method stores values through request, the request is also used on another page. getParameter (String name) is used to extract information. In this case, garbled characters are mainly caused by the encoding settings of the request storage information. When a post is submitted, if no encoding format is set for submission, it is submitted in iso8859-1 mode, and the accepted jsp is accepted in UTF-8 mode. So use the following statement to obtain a single correct Chinese String: String str = new String (request. getParameter ("something"). getBytes ("ISO-8859-1"), "UTF-8 ")
;

Solution:

Set request. setCharacterEncoding ("UTF-8") on the receive page "). It is best to set each page to request. setCharacterEncoding ("UTF-8") through a filter ").

3. garbled characters in get mode:

There are two methods to pass values through get, one is form get and the other is url address (in essence, both methods use url parameters to pass values ).

Get values in form mode:

The encoding process for get pass-through is as follows: first, the browser encodes the pass-through value based on the charset encoding method on the page, and then submits it to the server for tomcat. When tomcat decodes this information, the decoding method is implemented by the server. the URIEncoding settings in the xml file are determined, that is, when we use the request. the string obtained when getParameter ("") obtains the form parameter value, which is encoded by charset and decoded by URIEncoding.

As we know, as long as charset encoding is consistent with URIEncoding decoding and supports Chinese characters, no garbled characters can be found.

The method for setting URIEncoding is as follows:

Method 1:

Modify the $ TOMCAT/conf/server. xml file and add URIEncoding = "gbk" to the configuration of HTTP ctor or AJP Connector"

<... MaxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
EnableLookups = "false" redirectPort = "8443" acceptCount = "100"
ConnectionTimeout = "20000" disableUploadTimeout = "true" URIEncoding = "gbk"/>

Method 2:

Use useBodyEncodingForURI = "true". This method is suitable for running multiple Encoding programs in your TOMCAT instance.

<... MaxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
EnableLookups = "false" redirectPort = "8443" acceptCount = "100"
ConnectionTimeout = "20000" disableUploadTimeout = "true" useBodyEncodingForURI = "true"/>

EnableLookups = "false" redirectPort = "8443" protocol = "AJP/1.3" useBodyEncodingForURI = "true"/>

In Tomcat configuration, the Connector (HTTP Connector) attribute has a URIEncoding and
UseBodyEncodingForURI attribute. These two attributes set how to select character set encoding when URL Decoding is performed on additional parameters of the URL. URIEncoding is used to encode the character set of additional parameters after the URL. useBodyEncodingForURI indicates whether to use the character set encoding settings of the object content to replace the setting of URIEncoding. That is to say, when the useBodyEncodingForURI attribute is set. the character set encoding set by the setCharacterEncoding method also affects the URL Decoding result of getParameter and other methods on URL parameters. (At/% TomCat_Home %/
Find the <Connector> mark in the conf \ server. xml file, and add useBodyEncodingForURI = true to the end)

Garbled characters are transmitted using get in url mode:

In this way, the browser will not use the charset method of the page to encode the Chinese characters in the URL and then submit them to the server (IE and FireFox are all the same ), instead, the system uses GBK transcoding as the ISO-8859-1 and then submits it to the Server tomcat, so this process is:

First, the url address in the Chinese is converted from gbk to ISO-8859-1, handed to tomcat, and tomcat according to URLEcoding decoding, in this case, only the URLEcoding set to gbk in the request. getParameter ("") is not garbled. But this will affect the above configuration, so a good solution is to use java.net. URLEcoder and URLDecoder to manually encode and decode the Chinese characters in the address.

Therefore, the solution to the 10 thousand full solution is:

1. charset for all pages is set to UTF-8.

2. Tomcat's URIEncoding is a ISO-8859-1 by default, and I set it to a UTF-8, mainly to solve the problem of Chinese naming files and requests to submit in get mode possible garbled problem.

3. Add a filter and call the request. setCharacterEncoding ("UTF-8") method to set the request character set to UTF-8. This solves the issue of garbled requests submitted in post mode.

4. When the url address contains a Chinese parameter, URLEcoder is first encoded as UTF-8 for the Chinese parameter, and then URLDecoder is used to restore the request. getParameter ("") After receiving the parameter. For example:

From. jsp page:

<% String username = "Zhang xx ";

Username = URLEncoder. encode (username, "UTF-8 ");

%>

<A href = "to. jsp? Param = <% = username %> "> transfer in </a>

To. jsp page

<% = URLDecoder. decode (request. getParameter ("param"), "UTF-8") %>

In short, the garbled solution is as follows:

When the post value is garbled, Set request. setCharacterEncoding ("UTF-8") on the receiving end ")

When the get value or url is garbled, manually set the received parameter String str = new String (request. getParameter ("something"). getBytes ("ISO-8859-1"), "UTF-8 ");

The get and post values are different in Tomcat 5.

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.