2013-02-15 67 views
0

我使用兩種語言(英語和法語)製作表單。當有人輸入其名稱時,表單使用ajax從我們的數據庫查詢信息。可悲的是,當我使用response.getWriter()。write(「Heyéè」);功能似乎不支持像'é''è'這樣的字符。Ajax response.getWriter()不支持特殊字符

這裏是我的JavaScript和Ajax:

 function fillBlanks(idOfInputText,valueOfInputText) 
    { 

     //Prepare a new ajaxRequest. 
     if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp = new XMLHttpRequest(); 
     } else {// code for IE6, IE5 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     //Ajax receiving the response in this function 
     xmlhttp.onreadystatechange = function() 
     { 
      //state 4 is response ready. 
      //Status 200 is page found. 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
       //Change the content of the select to ajax result. 
       var content = []; 
       content = decode(xmlhttp.responseText); 

       document.getElementById('lastName').innerHTML = content[0]; 
       document.getElementById('firstName').innerHTML = content[1]; 
       document.getElementById('defaultPrinter').innerHTML = content[2]; 
       document.getElementById('dropDownRespExistant').innerHTML = content[3]; 

      } 
     }; 

     //Send the Ajax request. 
     xmlhttp.open('GET','mainServlet?command=ajax.FillBlanksHtml&ID='+ idOfInputText + '&VALUE=' + valueOfInputText, true); 
     xmlhttp.send(); 
    } 

這是我的java類:

public class FillBlanksHtmlCommand extends FrontCommand 
{ 
public static String userName; 
public static String lastName; 
public static String firstName; 

@Override 
public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
{ 
    String value = new String(request.getParameter("VALUE"));//get the ajax value 

    response.getWriter().write(OracleUserFinder.findHtmlCodeForOracleUser(value)); 


} 
} 

我想知道,如果它是posssible用另一方式發送特殊字符。如果你能告訴我這個函數使用什麼編碼,那會很好!

回答

2

根據問題Detecting the character encoding of an HTTP POST request,您應該將您的請求的字符編碼從ISO-8859-1更改爲UTF-8,以使您的請求正常工作。

最簡單的方法是(從問題的答案):

<form accept-charset="UTF-8"> 

雖然你可以使用任何其他解決方案的提出有。

對於響應,你只需要字符集添加到UTF-8如下所示:Setting the HTTP charset parameter

在servlet(或Controller類),只需添加以下代碼:

response.setContentType("text/xml; charset=UTF-8"); 
+0

謝謝你Luiggi!我只是添加了response.setcontentType,它完美地工作! – Benchy 2013-02-15 16:06:15