java web 页面url传值中文编码&解码问题
1、创建web项目:
2、2:在js中添加js:
function sssss(){
var act_id = ${gift.gid} ;
var user_id= ${sessionScope.user.id};
var title = "${gift.gname }";
//var cont = ${url };
var itype = "3";
var tiltes = encodeURI(encodeURI(title));
var url = "<%=path %>/share/tosharegift.action?act_id=" +act_id +"&user_id=" + user_id +"&title=" + tiltes +"&itype=" + itype ;
//+"&cont="+cont;
$.ajax({
url:url,
type:"post",
dataType:'json',
success:function(data){
if(data.success){
alert("分享成功!");
}else{
alert(data.errorMsg);
}
},
error:function(response){
alert("分享 失败!");
}
});
3、在action中进行解码:、
String title = getWebParams().getString("title");
String cont = getWebParams().getString("cont");
if(title != null){
//编码
try {
title = new String(title.getBytes("iso-8859-1"),"utf-8");
title = URLDecoder.decode(title, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}