您的位置:首页 > 编程语言 > Java开发

JAVAEE开发之POST/GET提交乱码解决总结

2011-11-13 23:07 656 查看
1,jsp表单提交到servlet默认为get提交;

2,get表单提交乱码处理最常见的是在tomcat服务器下把server.xml里面的8080端口后面加上URIEncoding="UTF-8",第二种方法是在后台得到get提交的参数后使用

String param=new String(request.getParameter("参数名").getBytes("iso-8859-1"),"UTF-8");

3,post表单提交乱码处理则为在后台使用方法request.setCharacterEncoding("UTF-8");即可。

4。超链接为GET提交,超链接提交出现乱码没有表单提交处理乱码哪么简单,要处理超链接提交,首先在server.xml里面增加UTIEnding="UTF-8"是必须的,然后若超链接中传递的参数为偶数个中文汉字,刚不须再处理乱码,若为奇数则用方法encodeURI('site.action?name=中文乱码');但是这样处理乱码有点麻烦为了解决不管参数的奇偶性也能处理乱码个 可以采用C标签:

<c:url value="login/login!login.action" var="url">

<c:param name="name" value="中文乱码s"></c:param>

</c:url>

<a href="${url}">提交</a>

5,jsp表单提交到action默认为post提交;POST提交不须再处理乱码因为struts已经在过滤器里面帮你把乱码处理了

6,ajax POST和GET提交乱码处理

在AJAX提交中要把参数和URL分离开来处理

var xmlHttp;

//自动加载国家

function country() {

var text = "sql=SELECT co_name FROM COUNTRY order by nvl(length(trim(co_name)),0) asc,co_name";

var url = "ajax/ajax!selectName.action";

createXmlhttp();

if (xmlHttp) {

xmlHttp.open("POST", url, true);

xmlHttp.setRequestHeader("Cache-Control", "no-cache");

xmlHttp.setRequestHeader("Content-Type",

"application/x-www-form-urlencoded");

xmlHttp.onreadystatechange = function() {

if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

parseMessage('country');

}

}

xmlHttp.send(text);

}

}

其中 xmlHttp.setRequestHeader("Cache-Control", "no-cache");

xmlHttp.setRequestHeader("Content-Type",

"application/x-www-form-urlencoded");这个要加上

action中:HttpServletResponse response= ServletActionContext.getResponse();

response.setContentType("html/xml;charset=gb2312");

response.setCharacterEncoding("UTF-8");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息