Be a positive person

Code, fix bugs, improve yourself

I have a paradise, facing programming, spring flowers!

I just used IDEA recently. I started to use Jetty as a container for Web project development while developing a function. The test passed. The local and server use the same container, this can avoid the local test because of different containers, but there is no reason for the online error ~! Just when I switched from Jetty to Tomcat, there was a problem, I accessed the JSP page and used response output, and garbled characters appeared!!

What is this? Cute money…)

����⣺ ���⣺ ���⣺ ���� �Copy the code

This is very depressed, this thought one-time test pass, did not expect to let me stay here again! See garbled code, don’t be afraid, don’t be nervous! Calm down and analyze to see what went wrong!

Start thinking:

0: database (mysql) — UTF-8

show variables like ‘character%’

character_set_client	utf8
character_set_connection	utf8
character_set_database	utf8
character_set_filesystem	binary
character_set_results	utf8
character_set_server	utf8
character_set_system	utf8
character_sets_dir	/aebiz/soft/mysql/share/charsets/
Copy the code

1: Tomcat – utf-8

<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8888" protocol="HTTP / 1.1" redirectPort="8443"/>
Copy the code

2:jsp — utf-8

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"% >Copy the code

3: web. XML – utf-8

<filter>
    <filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/ *</url-pattern>
</filter-mapping>
Copy the code

4: Request and Response

request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
Copy the code

5: Java code string encoding

str = tmpWriter.getByteArrayOutputStream().toString("utf-8");
Copy the code

After checking them all, they were all UTF-8. How can you still have a gibberish in an uppercase word that is all UTF-8?

I’m so sad. Is there any place that is not set? I wonder if there is something wrong with IDEA combined with Tomcat encoding setting. Follow this train of thought and find miracles ahead!

Add some parameters when configuring Tomcat. -dfile. encoding= utf-8

-server
-Xms1024m
-Xmx2048m
-XX:PermSize=512m
-XX:MaxPermSize=512m
-XX:+CMSClassUnloadingEnabled
-XX:+PrintGCDetails
-Xloggc:%M2_HOME%/gc.log
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=%M2_HOME%/java_pid.hproyuan
Copy the code

Use a search engine to find the following blog posts: Eclipse, IDEA setting JVM default encoding

So add VM parameter add

-Dfile.encoding=UTF-8

The configuration parameter is

-server
-Xms1024m
-Xmx2048m
-XX:PermSize=512m
-XX:MaxPermSize=512m
-XX:+CMSClassUnloadingEnabled
-XX:+PrintGCDetails
-Xloggc:%M2_HOME%/gc.log
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=%M2_HOME%/java_pid.hproyuan
-Dfile.encoding=UTF-8
Copy the code

Start found page garbled disappeared, but the console has garbled, garbled is endless ah!

Configure the bin folder in the idea installation directory in the idea.exe.vmoptions file

Add a configuration message: -dfile. encoding=UTF-8

So, everything worked out! Perfect!


Thank you for reading, if you think this blog is helpful to you, please like or like, let more people see! I wish you happy every day!



No matter what you do, as long as you stick to it, you will see the difference! On the road, neither humble nor pushy!

I wish you and I can become the best of themselves on the way of life, to become an independent person

© Alfayun who is getting better every day