You are already specifying "charset=utf-8"
for the generated HTML, so reading/rendering the data shouldn't be a problem in the browser (as you suggest).
But your console sample code is incorrect because it does not specify that UTF-8 is to be used. The default behavior will be to use the default encoding of your platform when creating the data, which is probably not what you want.
The simplest way to fix that in your sample code is to reassign System.out
to a PrintStream
that uses UTF-8 by calling setOut()
:
String text = "Muğla Sıtkı Koçman Üniversitesi";
System.out.println(text + " (default PrintStream)");
System.setOut(new PrintStream(System.out, true, "UTF8"));
System.out.println(text + " (UTF-8 PrintStream)");
However, if I run that code from the Windows Command Prompt I get this mess:
Mu?la S?tk? Koçman Üniversitesi (default PrintStream)
Muğla Sıtkı Koçman Üniversitesi (UTF-8 PrintStream)
The first line fails (like yours) because the data is being written and read using the default encoding, which is Cp437 on my machine. And the second line fails because although the data is being correctly written as UTF-8, it is still being rendered using Cp437.
To fix that, explicitly set your console's code page to UTF-8 by specifying chcp 65001
in the console before running your code (on Windows at least). Then you will see that the second line renders correctly, because it is both written and read as UTF-8:
Mu?la S?tk? Koman niversitesi (default PrintStream)
Muğla Sıtkı Koçman Üniversitesi (UTF-8 PrintStream)
Notes:
- You don't show how the generated HTML is created in your servlet, but if you ensure that it is being written as UTF-8 you should be OK.
- If you are still stuck, update your question to show the full source of your servlet, and if that is too large create a Minimal, Reproducible Example.
- I think it's unhelpful that Eclipse is doing things behind the scenes to allow the output to render correctly in its Console. I'm not sure why the Eclipse team decided to do that, because it masks an underlying issue in your code.