0

I'm making a form in tow languages (English and French). When someone enters its name, the form use ajax to query information from our database. Sadly when I use the response.getWriter().write("Hey é è"); function it doesn't seems to support characters like 'é' 'è'.

Here's my javascript and 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();
    }

and here's my java class :

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));


} 
}

I wanted to know if it was posssible to send special character with an other way. And if you can tell me what encoding this function uses, it would be nice!!

Benchy
  • 45
  • 7

2 Answers2

2

Based on the question Detecting the character encoding of an HTTP POST request, you should change the character encoding of your request from ISO-8859-1 to UTF-8 in order to make your request work.

The easiest way would be (from the answer of the question):

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

Although you can use any other of the solutions proposed there.

For the response, you just have to add the charset to UTF-8 as shown here: Setting the HTTP charset parameter

In your Servlet (or Controller class), just add this code:

response.setContentType("text/xml; charset=UTF-8");
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

how about using jQuery

contentType: "application/x-www-form-urlencoded; charset=UTF-8"

take a look at this link http://www.gunith.com/2010/11/how-to-send-german-umlauts-through-jquery-ajax/

grepit
  • 21,260
  • 6
  • 105
  • 81