Solution of output garbled code in IntelliJ IDEA

Posted by ctjansen on Mon, 08 Nov 2021 12:28:30 +0100

Recently, tomcat suddenly output garbled code on the console. All kinds of garbled code problems were solved by consulting a lot of data

IDEA console output garbled code

Problem 1: the tomcat console in idea outputs garbled code

  • Run the local tomcat\bin\start.bat file, and the page displays normally

  • Display garbled code in idea

  Add end teachers to get more programming materials for free

solve:

According to the information available on the Internet, make the following attempts

1. Modify the default encoding format of windbos

According to the information on the Internet, it may be the coding problem of windows, so;

 Copy code behind code
chcp #View the encoding format of the current cmd
chcp 65001   #Change to utf-8 code page
chcp 936       #Change to the default gbk
chcp 437       #American English

It seems useless< Font color ='Red '> if you find that the modification has no effect, you must modify it back before continuing the next attempt</ font>

2. Modification in idea

Because the output of tomcat's start.bat file is not garbled, I feel that the encoding in idea is not set properly. I make the following settings: Settings/Editor/File Encodings / set the encoding format to UTF-8

Help->Edit Customer VM Options
Add: - Dfile.encoding=UTF-8

Set the value of Additional command line parameters in java Complier,
-encoding=UTF-8. Unfortunately, it hasn't taken effect yet

3. Modification in Tomcat configuration file

The above schemes have been tried, but they still haven't solved my problem;
Therefore, under tomcat / conf / logging.properties:

Add or modify parameters   java.util.logging.ConsoleHandler.encoding = GBK

< font color ='Red '> if your console is not suddenly garbled, the above methods can basically solve your problem</ font>
Here I found that my environment is: This is already GBK, so the problem of console output garbled code is still not solved. Alas!
I tried to change the GBK here to UTF-8. Hey! At this time, my IDEA console output garbled code is solved!!! Shocked!!!
But!!!, I directly opened the output under tomcat\bin\start.bat, but there was garbled code!! I vomited. The garbled code on both sides can't be taken into account..... No way, I asked the teacher, because mine suddenly appeared. I should have accidentally modified the code. I don't know< Font color ='Red '> here you are reminded to go back if it doesn't work after modification, because you don't know the underlying code, so you can only try it slowly < / font >

Finally, a tomcat is reinstalled and the problem is solved!!!, 😵

Static resource page of web project in IDEA is garbled

Problem 2: HTML and JSP pages output garbled code


solve:

1.Html page garbled: add in the head:

 Copy code behind code
<META http-equiv=Content-Type content="text/html;charset=utf-8">

2. Add in JSP page:

 Copy code behind code
<%home.php?mod=space&uid=402414 page contentType="text/html;charset=utf-8" language="java"%>

3. In order to solve the problem of Chinese page data transmission, the following is added to web.xml:

 Copy code behind code
 <!--Chinese garbled code filter-->
  <!--Chinese garbled code filter-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

4. Set the mysql code to utf-8:

 Copy code behind code
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8

5. Set tomcat Code: to solve the problem that the parameters submitted in the get mode are garbled, add (only URIEncoding="UTF-8" needs to be added)

 Copy code behind code
<Connector port="8080" protocol="HTTP/1.1"   
              connectionTimeout="20000"   
              redirectPort="8443" URIEncoding="UTF-8" />  
<Connector port="8009" protocol="AJP/1.3"
              redirectPort="8443" 
               URIEncoding="UTF-8" />

6. tomcat server configuration in IDEA:

The coding of the above added header does not solve the problem; so

  • Set VM options in tomcat Server. The value is - Dfile.encoding=UTF-8. Unfortunately, it does not take effect
  • In tomcat Server, Java is useful_ TOOL_ OPTIONS=-Dfile.encoding\=UTF-8

    Finally, the html output is normal!!!
    But if we need configuration every time we start different projects, it's quite troublesome...
    Finally: in the tomcat\bin\catalina.bat file, add:
    set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF8

       Add end teachers to get more programming materials for free

Topics: Java Tomcat intellij-idea