The root cause of garbled
Source coding is inconsistent with the target encoding. The Chinese window system default encoding GBK, harms the number of programmers.
To minimize distortion, I personally think that to do five yards unity, IDE (Eclipse / idea), page (jsp / other template engines), application servers (tomcat, etc.), the source code (Java source code file and vicinity), database coding.
Eclipse is set to UTF-8
Eclipse.ini open Eclipse installation directory, add a line at the end of most
-Dfile.encoding=UTF-8
After modification, you can restart the eclipse.
JSP page encoding
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
tomcat coding
Open the tomcat installation directory under bin\setenv.bat
the file usually does not exist, create it, add the following
set JAVA_OPTS=-Dfile.encoding=UTF-8
Open conf\server.xml
, in 8080 the port Connector node belongs, adding URIEncoding, can solve most of the problems Chinese garbled GET request
URIEncoding="UTF-8"
Source code
Normally, the encoded file itself, depending on when a new file, IDE, or Project encoding.
Another hidden code, is maven / encoding used when compiling java source files ant
maven configuration is as follows
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Database coding
Generally speaking, if the other four yards resolved, under that most cases occur mysql encoding problem
There are four coding mysql
Server characterset: utf8 // 服务器字节集
Db characterset: utf8 // 当前数据集字节集
Client characterset: utf8 // 客户端字节集
Conn. characterset: utf8 // 当前连接的字节集
Modify the my.ini mysqld section, set up the server character set is the best solution
[mysqld]
character-set-server = utf8
However, for the existing system, the risk of global changes is relatively large, it is possible to solve the client, that is, to do configuration jdbcurl
jdbc:mysql://127.0.0.1/nutzdemo?useUnicode=true&characterEncoding=UTF-8
References:
1. coding and garbled ( Wendal Great God)