问题很简单:就是ajax返回的中文是乱码,用的weblogic,直接看代码吧,在线等,解决了立马给分jsp页面:
<%@ page contentType="text/html; charset=gbk"%>
js:function runAjax(url){
var str;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("POST",url, false);
req.onreadystatechange = function(){
if (req.readyState == 4) {
if (req.status == 200) {
str = req.responseText;
}
}
};
req.send();
}
if(str == null)
str = "";
return str.trim();
}
action:
public void getNsrxx(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
try {
String swdjh = request.getParameter("swdjh");
System.out.println("swdjh=="+swdjh);
NsrPo nsr = ggUtil.getNsrxxBySh(swdjh);
StringBuffer re = new StringBuffer();
if (nsr != null) {
re.append(nsr.getNsrMc()).append("&");
re.append(nsr.getFwsDm()).append("&");
re.append(nsr.getDh()).append("&");
re.append(nsr.getDz()).append("&");
} else {
re.append("false");
}
System.out.println("re=="+re);
response.getWriter().println(re);
} catch (Exception e) {
e.printStackTrace();
}
}过滤器:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response;
if (httpRequest.getCharacterEncoding() == null) {
httpRequest.setCharacterEncoding("gbk");
}
if (httpResponse.getCharacterEncoding() == null) {
httpResponse.setCharacterEncoding("gbk");
httpResponse.setContentType("text/html");
}
chain.doFilter(request, response); }
<%@ page contentType="text/html; charset=gbk"%>
js:function runAjax(url){
var str;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("POST",url, false);
req.onreadystatechange = function(){
if (req.readyState == 4) {
if (req.status == 200) {
str = req.responseText;
}
}
};
req.send();
}
if(str == null)
str = "";
return str.trim();
}
action:
public void getNsrxx(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
try {
String swdjh = request.getParameter("swdjh");
System.out.println("swdjh=="+swdjh);
NsrPo nsr = ggUtil.getNsrxxBySh(swdjh);
StringBuffer re = new StringBuffer();
if (nsr != null) {
re.append(nsr.getNsrMc()).append("&");
re.append(nsr.getFwsDm()).append("&");
re.append(nsr.getDh()).append("&");
re.append(nsr.getDz()).append("&");
} else {
re.append("false");
}
System.out.println("re=="+re);
response.getWriter().println(re);
} catch (Exception e) {
e.printStackTrace();
}
}过滤器:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response;
if (httpRequest.getCharacterEncoding() == null) {
httpRequest.setCharacterEncoding("gbk");
}
if (httpResponse.getCharacterEncoding() == null) {
httpResponse.setCharacterEncoding("gbk");
httpResponse.setContentType("text/html");
}
chain.doFilter(request, response); }
解决方案 »
- 如何解析一个txt文件,获取有用字段.
- 同一个SQL环境不同,SQL出错,不明白.
- js中alert()与showMessage()区别
- jfreechart K线图的X轴显示问题
- 求救:关于JavaBean的调用问题,一个简单的JavaBean,我就是调不出来,请高手指点
- 忘记了怎么在jsp里包含多个servlet了
- 如何用java实现网络存储,即网络硬盘的功能?
- struts下拉菜单问题?
- import Time; 语句出错!说缺少.,这问题已经耽误我快一周时间了,没法再顺利继续学下去,请帮忙!
- 简直是太怪了,ejb调用javabean出错?
- 关于struts2(看了一个小时没看出啥问题)
- 四色预警系统该如何实现?
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
try {
String swdjh = request.getParameter("swdjh");
System.out.println("swdjh=="+swdjh);
NsrPo nsr = ggUtil.getNsrxxBySh(swdjh);
StringBuffer re = new StringBuffer();
if (nsr != null) {
re.append(nsr.getNsrMc()).append("&");
re.append(nsr.getFwsDm()).append("&");
re.append(nsr.getDh()).append("&");
re.append(nsr.getDz()).append("&");
} else {
re.append("false");
}
System.out.println("re=="+re);
response.getWriter().println(re);
} catch (Exception e) {
e.printStackTrace();
}
}
你把re看中文是不是乱码
这个要不用个STRING的转码吧 response.getWriter().println(re);
这个re 不需要转成String 吗
设置成UTF-8也会报错? 什么错
String str=re.toString();
str=new String(str.getBytes("iso8859-1"),"utf-8");
System.out.println(str);你试试吧 希望可以帮你
写在send之前