I have a problem manipulating utf-8 strings in servlets. The output
appears garbled. For example, the servlet below should show a page
with the Chinese translation for 'logon', consisting of the two
characters ? (u\767b, deng) ? (u\5165, ru), but the result is garbled:
public void service(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
//PrintWriter out = response.getWriter();
ServletOutputStream out = response.getOutputStream();
out.println("\u767b\u5165");
Locale locale = new Locale("zh", "CN");
ResourceBundle bundle = ResourceBundle.getBundle("UTF8",
locale);
String country = bundle.getString("country");
out.println(country);
out.close();
}
The String 'country' (??) is looked up from a resource bundle and
printed correctly
in Chinese after it but not on the next line . What's up here?
The interesting thing is this: When I switch the
response.getOutputStream() line
with the commented out line response.getWriter() the opposite happens.
'Logon' is
now printed correctly and 'country' is garbled. Interesting.
The frustrating point is this: I am using xslt to translate xml to html
and the
whole lot seems to be garbled, whichever I use. I am using Apache
Xalan. The
same code will work if writing to a file from a stand-along app instead
of from
a servlet to a browser.
Alex Amies
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html