Several solutions to tomcat garbled characters

The problem of Chinese garbled characters in tomcat has the following situations:

1. Chinese garbled characters appear when starting tomcat through cmd (tomcat log output code is inconsistent with cmd code)

Chinese garbled characters appear when cmd starts tomcat

* Reason for garbled characters* : This is because the default encoding of cmd under windows is GBK encoding, and the default output of Tomcat console is set to UTF-8 encoding

The default encoding in the cmd property is GBK

Solution :

(Method 1) Modify the encoding format of the CMD command line to UTF-8

(Method 2) Change the tomcat console log output encoding format to GBK , and modify ConsoleHandler.encoding=utf-8 in the conf/logging.properties file in the tomcat root directory. This method can solve the Chinese garbled characters in the cmd console, but 不建议使用. Because the default encoding of tomcat has been changed, if we use idea to start tomcat, the default encoding of idea is not GBK, and the problem of garbled characters in tomcat under the idea console will also occur.

Modify the tomcat log configuration file

2. The problem of garbled characters under the idea console (idea console encoding is inconsistent with tomcat)

Solution :

First check the conf/logging.properties configuration file under tomcat

Check if ConsoleHandler.encoding=utf-8. If so, it means that there is no problem with the tomcat encoding. The problem is the idea encoding. If it is not equal to utf-8, change it to utf-8. Generally, the tomcat log can be output normally at this time information.

3. Chinese garbled characters in request parameters

request.getParameter()打印出来是乱码

Reason 1 : The idea console shows an encoding problem. The idea console shows that the default encoding of the jvm used is the GBK encoding of the win system. Change the jvm encoding format to UTF-8

//可以使用此方法测试自己的idea编码格式
import java.nio.charset.Charset;
public class Encoding {
    public static void main(String[] args) {
        System.out.println(Charset.defaultCharset().name());
    }
}

Solution:

Find the vmoptions configuration files of the two ideas and append them to the files-Dfile.encoding=UTF-8

Or directly modify the tomcat configuration in idea and add it in vm-options

-Dfile.encoding=UTF-8

, both have the same effect, both change the default GBK encoding of the system

Modify the 2 vm configuration files of idea

Modify tomcat settings vm-options

Reason 2 : After excluding console display problems, the rest is the problem generated during resource transmission

1. Check whether the encoding format of the JSP/HTML sent by the browser is UTF-8, if not, change it to UTF-8

Encoding of JSP files

2. Set the encoding of the server, the default is ISO-8859-1

Encoding the request header

request.setCharacterEncoding("UTF-8");

备注:该方法只对POST方式提交的数据有效,对GET方式提交的数据无效!

Set the encoding for the tomcat server server.xml file

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

备注:该方法对任何时候起作用

Encoding conversion of request parameters

String userName=request.getParamter("userName");
userName=new String(userName.getByte("iso-8859-1"),"utf-8");

4. Chinese garbled characters in response

Reason for garbled characters: The data sent by the server to the browser is encoded according to ISO-8859-1 by default. After receiving the data, the browser decodes the data according to the default character set and displays it. If the default decoding character set of the browser is not ISO-8859-1 , garbled characters appear.

To set the buffer encoding format to UTF-8, use the setCharacterEncoding() method.

//设置缓存区编码为UTF-8编码格式
response.setCharacterEncoding("UTF-8");

Actively tell the browser to use the UTF-8 encoding format to receive data in the response, using the setHeader() method.

//在响应中主动告诉浏览器使用UTF-8编码格式来接收数据
response.setHeader("Content-Type", "text/html;charset=UTF-8");

Use the encapsulation class to abbreviate Content-Type, and use the setContentType() method instead of using the setCharacterEncoding() method. This method is recommended.

//可以使用封装类简写Content-Type,使用该方法则无需使用setCharacterEncoding
response.setContentType("text/html;charset=UTF-8");

Reposted from: https://www.jianshu.com/p/7236d45cd1eb

Guess you like

Origin blog.csdn.net/fuhanghang/article/details/131320122