控制台打印乱码,IDEA设置的是UTF-8编码格式,响应解析也是按照UTF-8,唯独控制台输出乱码,话不多说看图:
原因是Tomcat设置的问题,修改下Tomcat配置即可:
点击
再这里加入这句话即可:
-Dfile.encoding=UTF-8
重启Tomcat后,控制台乱码问题得以解决。
添加IDEA配置。在IDEA顶部菜单找到 Help——>Edit Custom VM Options(如果是新UI在左侧菜单),添加如下配置:
-Dfile.encoding=UTF-8
在IDEA顶部菜单找到 File ——> Settings ——> File Encodings ,把相应的字符编码改成UTF-8即可。
重启IDEA。
Tomcat使用常见问题:
1 post请求乱码
通过HttpServletRequest设置请求编码:
/*处理post请求乱码*/
req.setCharacterEncoding("UTF-8");
2 get请求乱码
需要手动进行编码解码,或者设置tomcat中的server.xml中的URI编码。tomcat8已经解决了该问题,无需进行配置。
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8" />
2.2.3 响应乱码
通过HttpServletResponse设置响应编码
//以UTF-8编码处理数据
resp.setCharacterEncoding("UTF-8");
//设置响应头,以便浏览器知道以何种编码解析数据
resp.setContentType("text/html;charset=UTF-8");
2.2.4 默认欢迎页的设置
在tomcat中的conf/web.xml中配置了全局的几个默认欢迎页,当我们的页面文件为配置中的内容时,页面路径可以不用书写在url请求路径中欢迎页可以配置多个,匹配优先级自上而下
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
如果我们要在自己的项目中指定欢迎页,可以在自己的web.xml中配置欢迎页
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>