Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert to UTF-8.

 
Viet Jav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi frineds,
I have code fragment(JSP) as below, my aim is convert Unicode characters into UTF-8. Parameter "save" contains Unicode characters(Vietnamese characters) got from editor box on browser. These characters stored in String variable called save. Output of variable save(SAVE 1) is not correct that mean is not Vietnamese characters. So I convert them into UTF-8 in try/catch block. As a result of output(SAVE 2), they show Vietnamese characters correctly but my data outputed is missed many characters.
What are anything wrong? Please have a hints.
Thanks in advandce.
String save = request.getParameter("save");
System.out.println("SAVE 1 = "+save);
if (save != null) {
try {
save = new String(save.getBytes(),"UTF-8");
System.out.println("SAVE 2 = "+save);
} catch (UnsupportedEncodingException e) {
}
Vietjav.
 
Viet Jav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm sure that I using JSP that not support method:
void setCharacterEncoding(java.lang.String enc)
throws java.io.UnsupportedEncodingException
in Request object.
Have anyway to solve my trouble?
Vietjav.
 
Viet Jav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried and it is work now, the following is my code:
try {
byte[] utf8 = str.getBytes(request.getCharacterEncoding());
str = new String(utf8, enc);
} catch (UnsupportedEncodingException e) {
//System.out.println("UTF8-ERROR = "+e.toString());
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic