This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Convert to UTF-8. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Convert to UTF-8." Watch "Convert to UTF-8." New topic
Author

Convert to UTF-8.

Viet Jav
Ranch Hand

Joined: Dec 27, 2001
Posts: 55
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

Joined: Dec 27, 2001
Posts: 55
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

Joined: Dec 27, 2001
Posts: 55
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());
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert to UTF-8.