日期:2014-05-19  浏览次数:20736 次

表单提交编码问题 utf-8 你到底哪里去了。。。
讨论下面的一个问题,
用eclipse ee建立一个dynamic web program ,写一个页面index.jsp包含一个表单输入框name="data",然后添加一个servlet,把表单提交到servlet,然后获取data的内容,
请问我怎么能够获取以utf8编码的data的内容啊....

前提:
首选项的工作空间编码 utf8
jsp页面 contentType="text/html; charset=utf-8" pageEncoding="utf-8"
jsp的html内容 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
tomcat的conf/server.xml URIencoding="utf8"

请大牛们帮忙,我无论怎么操作都不能得到想要的结果
iso-8859-1 to utf-8 错 得到的是GBK的编码
iso-8859-1 to gbk 错 得到的还是GBK的编码...

求人关注,不明白我的问题可以提问,希望能够解决问题,先谢谢大家了;


------解决方案--------------------
1.在构造url是用javascript自带的encodeURIComponent方法将参数进行编码,下面是代码

var url = "handel.jsp?name="+encodeURIComponent(document.form1.name.value);
httpRequest = createHttpRequest();
httpRequest.onreadystatechange = aa;
httpRequest.open("GET",url,true);
httpRequest.send(null);

2.为了解决返回值XMLHttpRequest.resopnseText中文乱码的问题,需要设置下面这句话:

response.setHeader("Charset","UTF-8");


http://smallbottlecover.blog.hexun.com/10910911_d.html

------解决方案--------------------
这样试一下
String ss = new String(Request().getParameter("名").getBytes("ISO-8859-1"),"utf-8"));
或者
String ss = new String(Request().getParameter("名").getBytes("ISO-8859-1"),"gb2312"));

然后
ss.getBytes("utf-8");
------解决方案--------------------
ajax最好用post方式传值,再用response设置一下setheader()编码