0

I am trying to display below string at UI with UTF-8 Charset.

Controller Code

String str = "யாமறிந்த மொழிகளிலே தமிழ்மொழி போல் இனிதாவது எங்கும் காணோம், 
பாமரராய் விலங்குகளாய், உலகனைத்தும் இகழ்ச்சிசொலப் பான்மை கெட்டு, 
நாமமது தமிழரெனக் கொண்டு இங்கு வாழ்ந்திடுதல் நன்றோ? சொல்லீர்!
தேமதுரத் தமிழோசை உலகமெலாம் பரவும்வகை செய்தல் வேண்டும்.
";
PrintWriter pw = response.getWriter();
//pw.write(json.toString());
pw.write(new String(str.getBytes(Charset.forName("UTF-8"))));

JSP View

<meta charset="utf-8">
$('#doc1').html(data);

Output at UI:

யாமறிந�?த ம�?ழி�?ளில�? தமிழ�?ம�?ழி ப�?ல�? �?னிதாவத�? �?�?�?�?�?ம�? �?ாண�?ம�?, 

பாமரராய�? வில�?�?�?�?�?ளாய�?, �?ல�?ன�?த�?த�?ம�? �?�?ழ�?�?�?�?ி�?�?லப�? பான�?ம�? �?�?�?�?�?�?, 

நாமமத�? தமிழர�?ன�?�? �?�?ண�?�?�? �?�?�?�?�? வாழ�?ந�?தி�?�?தல�? நன�?ற�?? �?�?ல�?ல�?ர�?!

த�?மத�?ரத�? தமிழ�?�?�? �?ல�?ம�?லாம�? பரவ�?ம�?வ�?�? �?�?ய�?தல�? வ�?ண�?�?�?ம�?.
user3676578
  • 213
  • 1
  • 5
  • 17
  • 1
    What you are doing with `new String(...)` is incorrect. You get UTF-8 encoded bytes from a string and then you interpret them using the system's default charset (which may or may not be UTF-8). At best this will do nothing; at worst this will mangle the text or give you an exception. – Jesper Jul 25 '17 at 13:29
  • @Jesper pw.write(str); response Body : ???????? ????????? ????????? ???? ???????? ??????? ?????? pw.write(new String(str.getBytes(Charset.forName("UTF-8")))); response Body : யாமறிந�?த ம�?ழி�?ளில�? தமிழ�?ம�?ழி ப�?ல�? �?னிதாவத�? �?�?�?�?�?ம�? �?ாண�?ம�?, பாமரராய�? வில�?�?�?�?�?ளாய�?, �?ல�?ன�?த�?த�?ம�? �?�?ழ�?�?�?�?ி�?�?லப�? பான�?ம�? �?�?�?�?�?�?, நாமமத�? தமிழர�?ன�?�? �?�?ண�?�?�? �?�?�?�?�? வாழ�?ந�?தி�?�?தல�? நன�?ற�?? �?�?ல�?ல�?ர�?! த�?மத�?ரத�? தமிழ�?�?�? �?ல�?ம�?லாம�? பரவ�?ம�?வ�?�? �?�?ய�?தல�? வ�?ண�?�?�?ம�?. At least some of the character's are coming. – user3676578 Jul 25 '17 at 13:33
  • Yes. Don't do `new String(str.getBytes(Charset.forName("UTF-8")))`. It does not make sense. – Jesper Jul 25 '17 at 13:34
  • 1
    https://stackoverflow.com/questions/5729806/encode-string-to-utf-8 – user3676578 Jul 25 '17 at 13:37
  • Read that question carefully. It explains why what you are doing doesn't work... – Jesper Jul 25 '17 at 13:51
  • @Jesper Okay! I removed new String(str.getBytes(Charset.forName("UTF-8"))) but still at UI i am getting "�???????? ????????? ????????? ???? ???????? ??????? ??????" instead of actual letter's :( – user3676578 Jul 25 '17 at 14:07
  • This is a character encoding problem and the cause could be in many places, so it is difficult to help you fix this in an SO post. Is the browser interpreting the HTML correctly? Are you using a font which contains the characters for the text you are using? Etc. You'll have to carefully debug your program and follow exactly what's happening to the text at each step. – Jesper Jul 25 '17 at 14:19

0 Answers0