Tomcat get Chinese garbled

Source: Internet
Author: User

Garbled problem Reason:

The default tomcat is encoded with iso8859-1 when the URL is transmitted.

Solution One:

When using the Get transport parameters, the Chinese in the parameter is converted to the URL format, that is, the use of UrlEncode and urldecode to transfer, using this method is to convert the Chinese into a% beginning with the encoding in the URL transmission.

When using this method, pay attention to two points.

1. The front desk uses the UrlEncode, the corresponding use UrlDecode in the backstage.

2. The content using UrlEncode is empty within the parameters. It is important to note that he will convert the equal sign and so on. Therefore, it is better to first transfer the parameters after the splicing. Instead of stitching the URLs together and then converting them.

Solution Two:

Configure Tomcat to use the corresponding encoding that supports Chinese in the URL transfer process. General domestic likes to use GBK or gb2312. I personally recommend using Utf-8

In Tomcat's/conf/server.xml file, locate the following line.

<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/>

This line means using port 8080 to receive HTML requests. Here you can add a few parameters to configure different effects.

Uriencoding= "UTF-8" to set the encoding format for URL content when the URL is transmitted

Compression= "On" to turn on the compression function

Compressionminsize= "2048" enables compressed output content size, which defaults to 2KB

Nocompressionuseragents= "Gozilla, Traviata" for the following browsers, do not enable compression

Compressablemimetype= "Text/html,text/xml" compression type

For the solution garbled, change to the following line

<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443" URIEncoding= "UTF-8"/ >

Turn:

After analysis, I have a more in-depth understanding of Tomcat's handling mechanism and httpservletrequest.

Get parameter values in 1.filter filter garbled

Here is the code for the filter in the server side to get the parameters:

Java code
    1. Public void DoFilter (ServletRequest arg0, Servletresponse arg1,
    2. Filterchain arg2) throws IOException, servletexception {
    3. String foo=arg0.getparameter ("foo");
    4. System.out.println (foo);
    5. Arg2.dofilter (arg0, arg1);
    6. }

When I enter "http//:localhost:8080/rest/test?foo= China" in the browser, the browser will automatically do the "China" Uri transcoding, due to the use of the Chinese language environment, the browser will "China" transcoding "%d6%d0%b9%fa". " D6d0 "," B9FA "respectively" in "," state "of the GBK code. Equivalent to the following operations in the Java language

Java code
    1. Urlencoder.encode ("China","GBK")

The URL that is passed to the server is actually "HTTP//:LOCALHOST:8080/REST/TEST?FOO=%D6%D0%B9%FA".

Because Tomcat decodes the URL by default and uses the Iso-8859-1 character set, as shown below

Java code
    1. Urldecoder.decode ("%d6%d0%b9%fa","iso-8859-1");

Because encoding and decoding use different character sets, the decoded string is definitely not correct, and it is garbled to get the parameter value using the following method.

Java code
    1. String foo=request.getparameter ("foo");

Get parameter garbled in 2.resteasy service method

Java code
    1. @GET
    2. @Path ("/test")
    3. Public void Hello10 (@QueryParam (value="foo") String foo) {
    4. System.out.println (foo);
    5. }

The mechanism for obtaining the request parameter Foo in Resteasy is slightly different from the previous filter. The value of the Foo parameter is obtained by a resteasy framework similar to the following processing.

Java code
    1. String params=request.getquerystring ();
    2. SYSTEM.OUT.PRINTLN (params); //foo=%d6%d0%b9%fa
    3. String encodedparams= Urldecoder.decode (params,"UTF-8");
    4. ......

The parameters obtained by getquerystring () are not decoded by Tomcat, but are decoded by the Resteasy framework, which can be garbled if the incoming parameter is not UTF-8 encoded.

3. Summary

The parameters obtained using the Request.getparameter method are decoded by the Web server.

Raw parameters that are not decoded can be obtained using request.getquerystring

There are 2 ways to solve the garbled problem caused by Tomcat decoding.

Modify the Tomcat configuration file settings decoding method

Server side for the obtained parameter to the new String (Param.getbytes ("iso-8859-1"), "page Specify encoding") conversion

Tomcat get Chinese garbled

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.