when ajax receive information which include Chinese code from action of Struts2,ajax will show chinese code into confusion code,but if ajax receive information which include Chinese code from jsp, ajax will show correct Chinese code.I don't know why raise this error! How to correct it?
My code is follows: /*a.html saved as UTF-8 code*/ <HTML> <script language=javascript> var xmlhttp; function CreateXmlRequest(){ try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){ try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ try{ xmlhttp = new XMLHttpRequest(); } catch (e){ xmlhttp = false; } } } } function checkUserName(){ xmlhttp.open("get", "example/test.action", true); xmlhttp.onreadystatechange = updatePage; xmlhttp.send(null); } function updatePage(){ try{ if(xmlhttp.readyState == 4){ alert(xmlhttp.responseText); } } catch (e){} } CreateXmlRequest(); </script> <BODY> <input type=button value="button" onclick="checkUserName()"> </BODY> </HTML> /*HelloWorld.java saved as UTF-8 no BOM code,I compile as C:>javac -d . HelloWorld.java */ package example; import org.apache.struts2.ServletActionContext; import java.io.*; import javax.servlet.http.HttpServletResponse; public class HelloWorld extends ExampleSupport { public String test() throws Exception { HttpServletResponse response; response=ServletActionContext.getResponse(); response.setContentType("text/html;charset=gb2312"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pargma","no-cache"); response.setDateHeader("Expires",0); StringBuffer sb=new StringBuffer("<state>"); sb.append ("<city>北京</city><city>yangzhou</city><city>suzhou</city>"); sb.append("</state>"); PrintWriter out=response.getWriter(); out.write(sb.toString()); out.close(); return null; } } /*struts.xml*/ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <include file="example.xml"/> </struts> /*example.xml*/ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="example" namespace="/example" extends="struts-default"> <action name="test" class="example.HelloWorld" method="test"> <result>/example/a.jsp</result> </action> </package> </struts> When I run a.html,ajax should alert information "<state><city>北京</city><city>yangzhou</city><city>suzhou</city></state>",but it shows "<state><city>?confusion characters?</city><city>yangzhou</city><city>suzhou</city></state>" Anybody know how to do it? Thanks!