日期:2014-05-18  浏览次数:20674 次

AJAX/JSP 关于服务端返回中文乱码的问题…………
各种方法都试过了,服务端返回的中文还是乱码,各位大虾给帮帮忙阿~~~

"ajax.jsp"
<%@ page contentType = "text/html;charset=UTF-8" language = "java" %>

<html>
<head>
<META http-equiv ="Content-Type" content = "text/html; charset=UTF-8" >
</head>
<script type="text/javascript" >
var XMLHttpReq = false;

//creat XMLHttpRequest
function createXMLHttpRequest(){
if(window.XMLHttpRequest){ //Mozilla
XMLHttpReq = new XMLHttpRequest();
}else if(window.ActiveXObject){ //IE
try{
XMLHttpReq = new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
XMLHttpRequest = new ActiveXObject("Microsoft.XMLHttp");
}catch(e){
}
}
}
}

function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("GET", url, true);
XMLHttpReq.onreadystatechange = processResponse;
XMLHttpReq.send(null);
}

function processResponse(){
if(XMLHttpReq.readyState == 4){
if(XMLHttpReq.status == 200){
try{
var xmlDOM = XMLHttpReq.responseXML;
var root = xmlDOM.documentElement; 
var info = root.getElementsByTagName('res');
window.alert(info[0].firstChild.data);
document.login.username.value = "";
document.login.password.value = "";
}catch(e){
}
}else{
window.alert("请求错误");
}
}
}

function userCheck(){
var username = document.login.username.value;
var password = document.login.password.value;

if(username ==""){
window.alert("用户名不能为空");
document.login.username.focus();
document.login.password.value = "";
return false;
}else{

sendRequest('login.do?username=' + username + '&password=' + password);
}
}

function test(){
window.alert("test");
}
</script>

<body>
<form action="" method="post" name="login">
<p>
用户名:<input type="text" size="15" name="username"/>
</p>
<p>
密码:<input type="password" size="15" name="password"/>
</p>
<input type="button" value="登录" onclick="userCheck()"/>
</form>
</body>
</html>

"LoginAction.java"
import java.io.IOException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.io.PrintWriter;

public class LoginAction extends Action{
public ActionForward execute(
ActionMapping map, 
ActionForm form, 
HttpServletRequest request, 
HttpServletResponse response)
throws IOException{
String username = request.getParameter("username");
String password = request.getParameter("password");

PrintWriter out = response.getWriter();

response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml;charset=UTF-8");
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<response>");
if(username.equals("zhaolinisy") && password.equals("lovesy1117")){
out.println("<res>" + "欢迎您" + "<