Unicode characters in a servlet application appear as question marks

I recently implemented internationalization based on How to internationalize a Java web application? .

But now I am facing a problem on some pages. If it is English, it shows well, but if we choose any other language, then all the values ​​on this page will be shown as ????????? .

image

I checked the exceptions in the server logs, but I could not find anyone.

How is this caused and how can I solve it?

+2
source share
2 answers

Seeing ?????? instead of understandable characters (and even instead of Mojibake ) it usually indicates that responsible data transmission is itself very knowledgeable about the encoding used both at the source and at the destination. On average, a web application has only 2 places where it is: the point when data is transferred to / from the JDBC database and the point when data is transferred to the HTTP response from response.getWriter() (as JSP is implicitly used).

In your particular case with properties files, there is no means for the database, so the HTTP response remains the main suspect. This problem may occur if the server was not prompted to use UTF-8 to decode the characters that are written in the HTTP response, but instead uses some standard platform encoding, most often ISO-8859-1. Thus, any character in the source that is not covered by ISO-8859-1 will be turned into a question mark. Since ISO-8859-1 is exclusively for Latin characters, this will affect all non-Latin characters, such as Chinese, Japanese, Arabic, Cyrillic, Hebrew, Sanskrit, etc. They will all be written as question marks.

This can be fixed based on the JSP by adding the following to the very top of the JSP:

 <%@page pageEncoding="UTF-8" %> 

(note that you really need to put this in every JSP as well as the included files!)

Or, better, fix it based on the application by adding the following entry to webapp web.xml :

 <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-group> </jsp-config> 

See also:

+2
source

In fact, we are missing one line in the servlet before sending the response.

i.e. we forget to set the content type.

Here is the code to include in the servlet

 response.setContentType("text/html;charset=UTF-8"); 

thanks

0
source

All Articles