FreeMarker special character output as a question mark

I am trying to imagine a form with fields containing special characters, for example €ŠšŽžŒœŸ. As far as I can see on the ISO-8859-15 wikipedia page , these characters are included in the standard. Although the encoding for the request and response is set to ISO-8859-15, when I try to display the values ​​(using FreeMarker 2.3.18 in the JAVA EE environment), the values ???????. I set the accepted encoding form to ISO-8859-15, I checked that the form is submitted with the content type text/html;charset=ISO-8859-15(using firebug), but I cannot figure out how to display the correct characters. If I run the following code, the correct hex value is displayed (ex: Ÿ = be).

What am I missing? Thank you in advance!

System.out.println(Integer.toHexString(myString.charAt(i)));

EDIT:

I have the following code while processing the request:

PrintStream ps = new PrintStream(System.out, true, "ISO-8859-15");
String firstName = request.getParameter("firstName");

// check for null before
for (int i = 0; i < firstName.length(); i++) {
     ps.println(firstName.charAt(i)); // prints "?"
}

BufferedWriter file=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "ISO-8859-15"));
file.write(firstName); // writes "?" to file (checked with notepad++, correct encoding set) 
file.close();
+4
source share
1 answer

According to the hexadecimal value, the form data is transmitted correctly. The problem seems to be with the exit. Java replaces a character ?if it cannot be represented with the encoding used.

When building the output stream, you must use the correct encoding. What commands do you use for this? I don't know FreeMarker, but there will probably be something like

Writer out = new OutputStreamWriter(System.out);

This should be replaced with something resembling this:

Writer out = new OutputStreamWriter(System.out, "iso-8859-15");

By the way, UTF-8 is usually a much better choice for encoding encoding.

+2
source

Source: https://habr.com/ru/post/1543965/


All Articles