When you use the SPRINGMVC parameter to pass, troubleshoot a GET request with a Chinese garbled problem

Source: Internet
Author: User

Problem Description:


When you pass the SPRINGMVC parameter, when you encounter a GET request for Chinese information, the page answer displays a garbled language.

Workaround:

One, we need to Request.getparameter ("parameter name") to obtain the string first encoded into a byte stream with iso-8859-1, and then decoded it into a character stream with Utf-8, the code is as follows:

New String (request.getparameter ("parameter name"). GetBytes ("Iso-8859-1"), "Utf-8");

Second, we can also set the encoding by setting the URL encoding set (uriencoding) through the Tomcat configuration file.

In Tomcat's server.xml configuration file, add uriencoding= "UTF-8"

It is important to note that when you run the project file with Eclipse, if you change the server.xml file in the Tomcat installation directory , you will change The Server.xml file under Servers configured by Eclipse itself . Because this server is used by eclipse to run the project.

As shown in the following:

Add uriencoding= "UTF-8"

< Connector   ConnectionTimeout = "20000" Port = "8080" Protocol = "http/1.1" Redirectport = "8443" />

Modified to:

<uriencoding= "UTF-8"  connectiontimeout= "20000"  Port  = "8080"  protocol= "http/1.1"  redirectport= "8443"/>

Note:

The meanings of these two properties are explained below:

The Usebodyencodingforuri parameter indicates whether the data submitted by the URL and the data submitted in the form are re-encoded with the request.setcharacterencoding parameter, which is false by default.

The uriencoding parameter specifies a uniform recoding (decoding) encoding for all get mode requests.

The difference between uriencoding and Usebodyencodingforuri is that

Uriencoding is a uniform re-encoding of all requests for Get methods,

Usebodyencodingforuri is the re-encoding of the data according to the request.setcharacterencoding parameter of the page that should be requested, and different pages can have different recoding codes.

after configuring usebodyencodingforuri= "True" , you can solve the Chinese garbled problem of normal get requests, but the Chinese will still be garbled for GET requests initiated via Ajax. Please change usebodyencodingforuri= "true" to uriencoding= "UTF-8" .

When you use the SPRINGMVC parameter to pass, troubleshoot a GET request with a Chinese garbled 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.