页面传入后台出现汉字乱码解决办法java
1、若是使用iframe,将参数传入后台处理,若是出现汉字乱码安全
页面中的iframe:服务器
<iframe id="UserInfoIframe" name="UserInfoIframe" style="display: none"></iframe>post
js方法中:spa
var _$param="userName="+Name+"&userID="+Id;//Name为汉字.net
_$param=encodeURI(_$param);code
_$param=encodeURI(_$param);orm
var destUrl="/xxx/GetUserInfo_getInfo.action?"+_$param;get
document.getElementByIdx("UserInfoIframe").src=destUrl;input
后台处理:
String userName=request.getParameter("userName");
try {
userName=java.net.URLDecoder.decode(userName,"UTF-8");
}catch (UnsupportedEncodingException e) {
log4j.error(e.getMessage());
e.printStackTrace();
}
也能够:
js方法中不需处理;
后台:
String buttonName=request.getParameter("buttonName");
buttonName = new String(buttonName.getBytes("GBK"),"UTF-8");
2、经过form表单提交到后台
经过form传入后台,提交方式分为post和get两种。二者的区别须要经过提交表单后才看得出来,主要是在数据发送方式和接收方式上。Post和Get都是表单属性Method的可选值,Method的默认值为Get,二者的主要区别在于:
1.在客户端,Get方式在经过URL提交数据,提交后在地址栏中的地址会出现传入到后台的参数;而Post提交后地址栏中的地址不会出现参数。
2.在服务器端只能用Request.QueryString来获取Get方式提交来的数据,用Post方式提交的数据只能用Request.Form来获取。
通常来讲,尽可能避免使用Get方式提交表单,由于有可能会致使安全问题。好比说在登录表单中用Get方式,用户输入的用户名和密码将在地址栏中暴露无遗。可是在分页程序中,用Get方式就比用Post好。
Get把参数添加到action属性指定的地址中,并以锚方式打开。
Post经过HTTP post处理发送数据。
若是将form中的参数含有汉字,提交到后台有可能也会出现乱码,通常若是method设置为“post”,将不会出现乱码,若是将method设置为“get”,可能在汉字传入到后台后会出现乱码问题。
页面中的form:
<iframe id="userInfoIframe" name="userInfoIframe" style="display: none"></iframe>
<form id="userInfoForm" method="post" action="" target="userInfoIframe">
<input type="hidden" id="pageSize" name="pageSize" value=""/>
<input type="hidden" id="destPage" name="destPage" value=""/>
<input type="hidden" id="condition" name="condition" value=""/>
</form>