简体   繁体   中英

converting ISO-8859-1 to UTF-8 in servlet response

I have a servlet which retrieves data from an Oracle database. The encoding in the database is Latin1. Following code works fine for the browser:

resp.setCharacterEncoding("ISO-8859-1");
resp.getWriter().write(result);

but I need to be able to return UTF-8, like so:

utfResult = ???;
resp.setCharacterEncoding("UTF-8");
resp.getWriter().write(utfResult);

In order to achieve this I have tried many syntaxes, none of which worked.

The system is Tomcat 7 on Java 7. Thank you for your support.

To answer to the request for more detail:

with the first version, all the characters appear fine in the browser but not in the NodeJS program which uses the data.

when I replace the resp.setCharacterEncoding("ISO-8859-1"); with resp.setCharacterEncoding("UTF-8"); the character öäüèéà are replaced by ?

Java String is already Unicode text. So the only problem might be that the reading from the database could have gone wrong. Check that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM