Explanation of encoding garbled problem, the fastest and most effective way to solve Tomcat garbled. [Hope to correct me]

Self-understanding of coding garbled problem

The reason for the garbled code: the
  request URL must be encoded by the browser encoder ----> sent to the server and then decoded decoder --> the encoding and decoding methods are inconsistent, causing the garbled problem.

Pass in a parameter from the page username = 帝峰to deal with the garbled problem

@RequestMapping(path="testEncode")
    public String testEncode(String username) throws Exception{
    
    
	//直接输出结果是username = %E5%B8%9D%E5%B3%B0
	System.out.println("username = " + username);
	
    //查看浏览器请求域,发现username = 帝峰 这两个字被utf-8编码变成了%E5%B8%9D%E5%B3%B0
	//我们可以通过URLDecoder.decode解码(UTF-8编码)来解决中文乱码问题
        //String decodeName= URLDecoder.decode("%E5%B8%9D%E5%B3%B0","UTF-8");
        String decodeName= URLDecoder.decode(username,"UTF-8");
        //解码输出正常 username = 帝峰
        System.out.println("username = " + decodeName);
        return "requestmappingsuccess";
    }

The above kind URLDecoder.decode()of decoding obviously does not meet our needs, because we don't know which encoding is used in our encoding process, and it is very troublesome to write this way every time.

Let’s understand the execution process of the program [I feel there are some problems, please correct me]

jsp page —> translated (using pageEncoding encoding) into servlet storage ( jsp translation is run as a servlet ) —> through javaccompilation (javac compilation uses utf-8 encoding, which occupies two bytes, which is Unicode utf-8) becomes a servlet.class binary file —> hand it over to the tomcat server (tomcat uses iso8859-1 encoding by default, you need to change it to utf-8 to ensure that the encoding is consistent) —> hand it over to the Controller等program and set the encoding ontentType="text/html;charset=UTF-8"to UTF-8 —> To the client —> Then to the jsp page —> pageEncoding="UTF-8"Render the page to the user through decoding.

[The above summary is my own summary, I feel that there are some mistakes, the following points out that I am
easy to change, cute new one~] The easiest way to solve the garbled code is to ensure that each step of the execution step is in the same encoding format. That is to ensure that all environment codes are consistent.

The easiest way to solve the garbled output of the Tomcat server console:

Screenshot of garbled code

Insert picture description here

Solution

  Modify the logging.propertiesfiles in the conf directory under the tomcat directory第47行,将UTF-8改为GBK

Insert picture description here

Insert picture description here

[Summary-Doubts]

I modified the original one step by step to solve the garbled code. As for why this was solved step by step, I also want to know, hope the big guys will tell me

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/109082850