Human Being
Human Being

Reputation: 8387

UTF-8 issue in sending local language message in chat application?

In my Chat application I need to send message in local language.

It works fine ,when trying to send a message to agent and displayed exactly it in the Customer Side.

But at agent side in displays நà¯à®à¯à®à®³à¯ à®à®ªà¯à®ªà®à®¿ à®à®°à¯à®à¯à®à®¿à®±à¯à®°à¯à®à®³à¯ for நீங்கள் எப்படி இருக்கிறீர்கள்(in english - how are you)...

In my jsp page I used,

<%@ page contentType="text/html; charset=UTF-8"%> 
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8; ">
<title>Initializer</title>
<link rel="stylesheet" type="text/css" href="css/login.css" />
<script type="text/javascript" src="javascript/login.js"></script>
</head>
<body>

<div><textarea id="textarea" rows="10" readonly></textarea></div>
<div>
<table>
    <tr>
        <td id="msg" class="fontStyle">message :</td>
        <td id="msgvalue"><input size="40" type="text" id="message"
            onkeydown="enterKey()" /></td>
        <td style="width: 5%;"></td>
        <td>
        <center><input type="button" id="button3"
            onclick="sendMessage()" value="Send" /></center>
        </td>
    </tr>
</table>
</div>
</div>
</body>

In java script,

function sendMessage(){
    clearInterval(timer);
    timer = setInterval(function(){pingAction();},5000);
    message = document.getElementById("message").value;
    document.getElementById("message").innerText = "";
    //document.getElementById("textarea").innerHTML = message;
    //alert("---------->"+sessionId+userId+secureKey+message);
    //alert("msg ---> : "+ message);
    try
    {
        xmlhttp.onreadystatechange=function()
        {
            //alert("Status : "+xmlhttp.status+"\nreadyState : "+xmlhttp.readyState);
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                //alert("Message ----> : "+xmlhttp.responseText.toString());
                //alert("Decoded Message----> : "+decodeURIComponent(xmlhttp.responseText.toString()));
                //var checkMsg = decodeURIComponent(xmlhttp.responseText.toString());
                var checkMsg = xmlhttp.responseText.toString();
                if(checkMsg != "null" && checkMsg != null){
                    //document.getElementById("textarea").innerHTML +=  checkMsg;
                    if(textarea.value == "")
                        textarea.value = checkMsg;
                    else
                        textarea.value += "\n"+ checkMsg;
                }
            }
        };

        xmlhttp.open("POST","SendMessageAction?sessionId="+sessionId+"&userId="+userId+"&securekey="+secureKey+"&message="+encodeURIComponent(message)+"&sid="+Math.random(),true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("charset","UTF-8");        
        xmlhttp.send();

    }
    catch(err)
    {
        alert(err.description);
    }
}

In my servlet,

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        PrintWriter out = response.getWriter();

        msg = request.getParameter("message");
        //System.out.println("msg -----> : "+msg);
        seckey = request.getParameter("securekey");
        uid = request.getParameter("userId");
        sessionId = request.getParameter("sessionId");
        //counter =Integer.parseInt(request.getParameter("counter"));
        counter = 1;
        protocol = ApplicationInfo.flexProtocol;

        message = new SendMessage();
        message.send(msg, seckey, uid, sessionId, counter, protocol);

        CustomerInfo customer = ApplicationInfo.customerDetails.get(uid);

        out.print(customer.getMessage());


    }

Thanks in advance..

Upvotes: 1

Views: 723

Answers (1)

BalusC
BalusC

Reputation: 1109292

But at agent side in displays நà¯à®à¯à®à®³à¯ à®à®ªà¯à®ªà®à®¿ à®à®°à¯à®à¯à®à®¿à®±à¯à®°à¯à®à®³à¯ for நீங்கள் எப்படி இருக்கிறீர்கள்(in english - how are you)...

You will get exactly this result when ISO-8859-1 is incorrectly been used to decode the given characters to bytes before transmitting them over the HTTP line, or when the client is incorrectly been instructed to ust ISO-8859-1 to encode the retrieved bytes to characters. Here's the evidence:

System.out.println(new String("நீங்கள் எப்படி இருக்கிறீர்கள்".getBytes("UTF-8"), "ISO-8859-1"));

You basically need to instruct the servlet container to use UTF-8 to decode the given characters to bytes and the client to use UTF-8 to encode the retrieved bytes to characters. It would otherwise default to ISO-8859-1. This can be done in the servlet as follows, before you write anything to the response:

response.setCharacterEncoding("UTF-8");

See also:


Unrelated to the concrete problem, you've other potential encoding problems; you haven't instructed your JSP to render the response using UTF-8 as well. Only setting the @page contentType is not sufficient, you also need to set the @page pageEncoding. Even more, setting alone it is already sufficient as JSP already defaults to text/html.

<%@ page pageEncoding="UTF-8"%> 

Once you've fixed that, then you should add another line to the servlet code, before the first request.getParameter() call ever in order to instruct the container to use UTF-8 to encode the request parameters.

request.setCharacterEncoding("UTF-8");

Also note that the HTML <meta> tag is not been used when the page is served over HTTP. It's plain ignored. It's only been used when the enduser saves the HTML output to a HTML file on disk and then reopens it from disk by file:// URI.

Upvotes: 2

Related Questions