tomcat乱码问题
- 乱码问题都是由于字符编码不匹配导致
- Windows 中文系统默认编码为GBK 而一般应用系统开发都是UTF-8 这样如果运行应用系统没有配置字符编码使用了系统默认编码就会导致乱码
1 日志乱码
由于日志文件内容大部分字符都是ASCII 所以推荐使用UTF-8来存储,节省空间
打印日志乱码
需要配置的文件有
%tomcat_home%/conf/logging.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 1catalina.org.apache.juli.AsyncFileHandler.level = FINE 1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina. 1catalina.org.apache.juli.AsyncFileHandler.encoding = UTF-8
2localhost.org.apache.juli.AsyncFileHandler.level = FINE 2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost. 2localhost.org.apache.juli.AsyncFileHandler.encoding = UTF-8
3manager.org.apache.juli.AsyncFileHandler.level = FINE 3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 3manager.org.apache.juli.AsyncFileHandler.prefix = manager. 3manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
4host-manager.org.apache.juli.AsyncFileHandler.level = FINE 4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager. 4host-manager.org.apache.juli.AsyncFileHandler.encoding = UTF-8
java.util.logging.ConsoleHandler.level = FINE java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter java.util.logging.ConsoleHandler.encoding = UTF-8
|
2 页面乱码
web项目运行起来后页面乱码
需要配置
%tomcat_home%/conf/server.xml
1 2 3 4
| <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
|
%tomcat_home%/conf/web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>fileEncoding</param-name> <param-value>UTF-8</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
|
如果是tomcat下载的是zip解压就可以运行的需要改配置
%tomcat_home%/bin/catalina.bat
1
| set JAVA_OPTS=-Xms512m -Xms1024m -XX:MaxPermSize=1024m -Dfile.encoding=UTF-8
|
如果是Windows安装服务的方式运行需要添加配置
%tomcat_home%/bin/Tomcat-8.5.73w.exe