52,797
社区成员
发帖
与我相关
我的任务
分享
ServletActionContext.getResponse().getWriter().print("你好")
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
var retText = xmlhttp.responseText;
alert(retText); -----------------------------------------中文乱码了,变成了???
}
}
js传参要解决乱码
xmlhttp.open("GET","login.action?strName=" + encodeURIComponent(strUserName) ,true);
or
xmlhttp.open("GET","login.action?strName=" + encodeURI(encodeURI(strUserName)) ,true);
这样中文参数就被编码传过去了
public String execute() throws Exception {
//String strName= (String)ServletActionContext.getRequest().getAttribute("strName"); -----问题。。。
//获取参数的方式不对
//1、利用strtus2的特性,设置strName的getter、setter方法就会自动帮我们获取或设置参数
//2、用getParameter获取参数
String strName= (String)ServletActionContext.getRequest().getParameter("strName"); //这种方式侵入了servlet的api,不建议使用。用setter比较好
strName = URLEncoder.encode(strName , "utf-8");//前台参数进行转码,这里也需要解码
if(!"".equals(strName) && strName!= null){
ServletActionContext.getResponse().getWriter().print("欢迎"+strName);
}
return null;
}
responseText乱码设置response编码格式
response.setCharacterEncoding("utf-8")
request.setCharacterEncoding("utf-8")