Tomcat后台中文乱码分析

拈花ヽ惹草 提交于 2019-11-29 00:46:33

web应用程序在提交中文数据的时候,后台经常出现乱码现象。为了彻底分析产生原因,做了一个简单测试:

创建一个简单的java web应用程序,没有使用任何框架,部署在tomcat环境中。

使用谷歌和ie浏览器分别进行测试,测试结果如下:

Browser Method Tomcat URIEncoding httpServletRequest System.out
Chrome GET - - (乱码)
Chrome GET - setCharacterEncoding("gb2312") (乱码)
Chrome GET - setCharacterEncoding("utf-8") (乱码)
Chrome GET URIEncoding="gb2312"  - (乱码)
Chrome GET URIEncoding="gb2312"  setCharacterEncoding("gb2312") (乱码)
Chrome GET URIEncoding="gb2312"  setCharacterEncoding("utf-8") (乱码)
Chrome GET URIEncoding="utf-8"  - 中文正常显示
Chrome GET URIEncoding="utf-8"  setCharacterEncoding("gb2312") 中文正常显示
Chrome GET URIEncoding="utf-8"  setCharacterEncoding("utf-8") 中文正常显示
Chrome POST - - (乱码)
Chrome POST - setCharacterEncoding("gb2312") (乱码)
Chrome POST - setCharacterEncoding("utf-8") 中文正常显示
Chrome POST URIEncoding="gb2312"  - (乱码)
Chrome POST URIEncoding="gb2312"  setCharacterEncoding("gb2312") (乱码)
Chrome POST URIEncoding="gb2312"  setCharacterEncoding("utf-8") 中文正常显示
Chrome POST URIEncoding="utf-8"  - (乱码)
Chrome POST URIEncoding="utf-8"  setCharacterEncoding("gb2312") (乱码)
Chrome POST URIEncoding="utf-8"  setCharacterEncoding("utf-8") 中文正常显示
IE11 GET - - (乱码)
IE11 GET - setCharacterEncoding("gb2312") (乱码)
IE11 GET - setCharacterEncoding("utf-8") (乱码)
IE11 GET URIEncoding="gb2312"  - (乱码)
IE11 GET URIEncoding="gb2312"  setCharacterEncoding("gb2312") (乱码)
IE11 GET URIEncoding="gb2312"  setCharacterEncoding("utf-8") (乱码)
IE11 GET URIEncoding="utf-8"  - 中文正常显示
IE11 GET URIEncoding="utf-8"  setCharacterEncoding("gb2312") 中文正常显示
IE11 GET URIEncoding="utf-8"  setCharacterEncoding("utf-8") 中文正常显示
IE11 POST - - (乱码)
IE11 POST - setCharacterEncoding("gb2312") (乱码)
IE11 POST - setCharacterEncoding("utf-8") 中文正常显示
IE11 POST URIEncoding="gb2312"  - (乱码)
IE11 POST URIEncoding="gb2312"  setCharacterEncoding("gb2312") (乱码)
IE11 POST URIEncoding="gb2312"  setCharacterEncoding("utf-8") 中文正常显示
IE11 POST URIEncoding="utf-8"  - (乱码)
IE11 POST URIEncoding="utf-8"  setCharacterEncoding("gb2312") (乱码)
IE11 POST URIEncoding="utf-8"  setCharacterEncoding("utf-8") 中文正常显示

从上面的测试结果看出:

1.如果是GET请求,应在tomcat的server.xml中设置 URLEncoding="UTF-8" 。

2.如果是POST请求,请在后台设置 httpServletRequest.setCharacterEncoding("utf-8"); 或通过使用MVC框架并设置字符编码参数来解决乱码问题。

以下是测试页面代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>字符编码测试</title>
</head>
<body>
<form name="posttest" action="/posttest" method="post">
提交测试(POST): 
<input type="text" name="user" value="中文正常显示" />
<input type="submit" value="Submit" />
</form>
<br />
<a href="/gettest?user=中文正常显示">请点击这里进行GET测试</a>
</body>
</html>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!