URL传值中文乱码的解决
使用 tomcat 时,相信大家都回遇到中文乱码的问题,具体表现为通过表单取得的中文数据为乱码。
一、初级解决方法 通过一番检索后,许多人采用了如下办法,首先对取得字符串按照 iso8859-1 进行解码转换,然后再按照 gb2312 进行编码,最后得到正确的内容。
示例代码如下:
http://xxx.do?ptname='我是中国人'
String strPtname = request.getParameter("ptname");
strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");
String para = new String( request.getParameter("para").getBytes("iso8859-1"), "gb2312");
具体的原因是因为美国人在写 tomcat 时默认使用 iso8859-1 进行编码造成的。
然而,在我们的 servlet 和 jsp 页面中有大量的参数需要进行传递,这样转换的话会带来大量的转换代码,非常不便。
二、入门级解决方法 后来,大家开始写一个过滤器,在取得客户端传过来的参数之前,通过过滤器首先将取得的参数编码设定为 gb2312 ,然后就可以直接使用 getParameter 取得正确的参数了。这个过滤器在 tomcat 的示例代码 jsp-examples 中有详细的使用示例, 其中过滤器在 web.xml 中的设定如下,示例中使用的是日文的编码,我们只要修改为 gb2312 即可 view plaincopy to clipboardprint? Set Character Encoding filters.SetCharacterEncodingFilter encoding EUC_JP Set Character Encoding filters.SetCharacterEncodingFilter encoding EUC_JP
过滤器的代码如下:
view plaincopy to clipboardprint?
public class SetCharacterEncodingFilter implements Filter
{ // 编码的字符串
protected String encoding = null; // 过滤器的配置
protected FilterConfig filterConfig = null; // 是否忽略客户端的编码
protected boolean ignore = true; // 销毁过滤器
public void destroy(){
this.encoding = null;
this.filterConfig = null; }
// 过滤方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 如果使用过滤器,忽略客户端的编码,那么使用通过过滤器设定编码
if (ignore || (request.getCharacterEncoding() == null))
{ String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding); }
// 传送给下一个过滤器
chain.doFilter(request, response); }
// 初始化过滤器
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true; else this.ignore = false; }
// 返回过滤器设定的编码
protected String selectEncoding(ServletRequest request)
{ return (this.encoding); } }
public class SetCharacterEncodingFilter implements Filter{
// 编码的字符串
protected String encoding = null; // 过滤器的配置
protected FilterConfig filterConfig = null; // 是否忽略客户端的编码
protected boolean ignore = true; // 销毁过滤器
public void destroy() {
this.encoding = null;
this.filterConfig = null; }
// 过滤方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 如果使用过滤器,忽略客户端的编码,那么使用通过过滤器设定编码
if (ignore || (request.getCharacterEncoding() == null))
{ String encoding = selectEncoding(request);
if (encoding != null) request.setCharacterEncoding(encoding); }
// 传送给下一个过滤器
chain.doFilter(request, response); }
// 初始化过滤器
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) this.ignore = true;
else if (value.equalsIgnoreCase("true")) this.ignore = true;
else if (value.equalsIgnoreCase("yes")) this.ignore = true;
else this.ignore = false; }
// 返回过滤器设定的编码
protected String selectEncoding(ServletRequest request)
{ return (this.encoding); } }
然而在 tomcat5 中,即使使用过滤器,仍然可能取得乱码,原因何在呢?
三、高级解决方法
这是因为,在 tomcat4 和 tomcat5 中对参数的处理是不一样的,在 tomcat4 中 get 与 post 的编码是一样的,所以只要在过滤器中通过 request.setCharacterEncoding 设定一次就可以解决 get 与 post 的问题。
然而,在 tomcat5 中,get 与 post 的处理是分开进行的 在 tomcat 5 中,为了解决编码问题,tomcat 的作者作了很多努力,具体表现为在 tomcat 的配置文件 server.xml 中对 Connector 元素增加了如下的配置参数,专门用来对编码进行直接的配置 URIEncoding 用来设定通过 URI 传递的内容使用的编码,tomcat 将使用这里指定的编码对客户端传送的内容进行编码。
什么是 URI 呢?
java doc 的说明中如下说明:URI 是统一资源标识符,而 URL 是统一资源定位符。
因此,笼统地说,每个 URL 都是 URI,但不一定每个 URI 都是 URL。这是因为 URI 还包括一个子类,即统一资源名称 (URN),它命名资源但不指定如何定位资源。 也就是说,我们通过 get 方法提交的参数实际上都是通过 uri 提交的,都由这个参数管理,如果没有设定这个参数,则 tomcat 将使用默认的 iso8859-1 对客户端的内容进行编码。 useBodyEncodingForURI 使用与 Body 一样的编码来处理 URI, 这个设定是为了与 tomcat4保持兼容,原来在 tomcat4 和 tomcat5 中队参数的处理是不一样的,在 tomcat4 中 get 与 post 的编码是一样的,所以只要在过滤器中通过 request.setCharacterEncoding 设定一次就可以解决 get 与 post 的问题。
然而,在 tomcat5 中,get 与 post 的处理是分开进行的,对 get 的处理通过 前面的 URIEncoding 进行处理,对 post 的内容依然通过 request.setCharacterEncoding 处理,为了保持兼容,就有了这个设定。 将 useBodyEncodingForURI 设定为真后,就可以通过 request.setCharacterEncoding 直接解决 get 和 post 中的乱码问题。 这样,我们可以通过在 server.xml 中设定 URIEncoding 来解决 get 方法中的参数问题,使用过滤器来解决 post 方法中的问题。 或者也可以通过在 server.xml 中设定 useBodyEncodingForURI 为 true ,配合过滤器来解决编码的问题。 在这里,我强烈建议在网站的创作过程中,全程使用 utf-8 编码来彻底解决乱码问题。
具体操作如下:
1、页面内容使用 utf-8 格式保存,在页面中加入
<mete http-equiv="contentType" content="textml;charst=utf-8">
2、服务器端的 server.xml 中设定 useBodyEncodingForURI = true
3、使用过滤器,过滤器设定编码为 utf-8
四:如果有一些转码也转不过来的话,可是试试打开tomcat的server.xml,找到并在最后加上useBodyEncodingForURI="true" URIEncoding="UTF-8",如下
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="80" redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8">
五: 如果用jstl的话,可以自己写一个el的function,调用URLEncoder.encode来编码。
IE缺省对URL后面的参数是不编码发送的,但是tomat缺省是按ISO8859-1来进行URL解码,因此才会出现上述错误。好的做法是:
1、在URL参数中确保用UTF-8编码之,方法可以用js函数encodeURI(),或调用自定义的el function;
2、设置server.xml中的Connector熟悉URIEncoding="UTF-8",确保解码格式与编码格式统一;
方法四: view plaincopy to clipboardprint?
在action中,String s=request.getParameter("s"); s=new String(s.getBytes("iso-8859-1"),"gbk");
六:js的乱码解决
1.客户端: url=encodeURI(url);
服务器: String linename = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
2.客户端: url=encodeURI(encodeURI(url)); //用了2次encodeURI
服务器: String linename = request.getParameter(name); //java : 字符解码
linename = java.net.URLDecoder.decode(linename , "UTF-8");
URL传值中文乱码的解决的更多相关文章
- js url传值中文乱码完美解决(JAVA)
js url传值中文乱码完美解决(JAVA) 首先在你的jsp页面这样更改: var url="你要传入的Action的位置&ipid="+ipid+"& ...
- java 页面url传值中文乱码的解决方法
parent.window.location.href 和 iframe中src的乱码问题.要在这两个url地址中传中文,必须加编码,然后再解码.编码:encodeURI(encodeURI(&quo ...
- js url传值中文乱码之解决之道
在websphere 中使用的是url=encodeURI(encodeURI(url)); //用了2次encodeURI 测试成功,第一次转换没有尝试, 处理方法一. js 程序代码:url=en ...
- URL传值中文乱码
url含有中文 先encodeURI(url)编码 获取之后再解码decodeURI //加密 var param = "itname=" + slRows.ITNAME + &q ...
- 解决Jsp与Java后台之间url传值中文乱码问题
JSP页面中,由JavaScript拼接url时,对于中文名称变量应该加上encodeURIComponent方法,对中文进行十六进制编码. 例如: url = /com/xxx/B.jsp?chin ...
- jquery的ajax()函数传值中文乱码解决方法介绍
jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...
- ajax()函数传值中文乱码解决方法介绍
jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 复制代码 代码如下: $.ajax({ dataType : ‘json',type : ‘POST',url : ‘ht ...
- [转]IE、FireFox、Chrome浏览器中关于URL传参中文乱码,解决兼容性问题!
原文地址:https://cloud.tencent.com/developer/article/1334736 前台用url传值中文,后台用request.getParameter接收参数.在Fir ...
- 解决get方法传递URL参数中文乱码问题
[转]解决get方法传递URL参数中文乱码问题 来自:http://www.javaeye.com/topic/483158 应用一:解决tomcat下中文乱码问题(先来个简单的) 在tomcat下, ...
随机推荐
- 使用Jmeter进行接口测试和压力测试的配置和使用
1. Jmeter简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. JMeter 可 ...
- ZT-Android深入浅出之Binder机 制
转贴 不是原创! Android深入浅出之Binder机 制 一说明 Android系统最常见也是初学者最难搞明白的就是Binder了,很多很多的Service就是通过Binder机制来和客户端通讯交 ...
- Windows环境双系统安装环境配置
(最惊喜的事情莫过于...在安装系统完成重新试图安装Docker时解决了关于HyperV的问题,结果提示Docker只能在Win10 Pro或者Enterprise环境下运行...我很坚强...可以按 ...
- Thread-Specific-Storage for C/C++
引用出处:https://www.cse.wustl.edu/~schmidt/PDF/TSS-pattern.pdf 摘要: 理论上多线程会提高程序性能,但实际上,由于在获取和释放锁的开销,多线程经 ...
- 关于PHP数组你应该知道的事情
(1).PHP数组的遍历顺序 先举个栗子: <?php $arr['a'] = '123'; $arr['b'] = '456'; $arr['c'] = '789'; foreach($a a ...
- BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡:贪心【最小匹配代价】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3399 题意: 给你一个数列a,和一个可变换顺序的序列b(数列长度≤25000). a增加一 ...
- TunSafe使用分享
体验还是很棒的,关于使用中遇到的一些问题在此分享下 官网打不开? 这个真的很不解,科学以后遇到的, 后来在手机上同样的环境测试可以打开 最后居然使用ie浏览器解决了,看来是国产浏览器在作怪 ie终于打 ...
- 【转】Nginx反向代理转发tomcat
http://blog.csdn.net/mlc1218559742/article/details/53117520 最近刚接触nginx,在网上查阅了相关资料,看到最多的形容nginx的词就是反向 ...
- [转]Javascript 取小数点后面N位
用Javascript取float型小数点后两位,例22.127456取成22.13,如何做? 1. 最笨的办法....... [我就怎么干的.........] } 2. 正则表达式效果不错 &l ...
- 【题解】洛谷P1541 [NOIP2010TG] 乌龟棋(类似背包的DP)
题目来源:洛谷P1541 思路 类似背包的题 总之就是四种卡牌取的先后顺序不同导致的最终ans不同 所以我们用一个四维数组每一维分别表示第几种取了几张的最大分数 然后就是简单DP解决 代码 #incl ...