Display Latin characters in a String

6

How do I display Latino characters as content for a web page?

This content to be displayed is written in java, which should encode it correctly and return a String with values to be written to the server.

I'm using the URLEncoder class of java.

 news = URLEncoder.encode(newsString, "UTF-8");
    
asked by anonymous 30.09.2015 / 19:00

1 answer

5

If the coding of your page is correct, for example when sending the HTTP header:

Content-Type: text/html; charset=utf-8

Or by specifying it in the HTML itself:

<meta charset="utf-8" /> 

or

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Then at first you do not have to do anything: the String class already works with Unicode characters (being represented internally by UTF-16 if I'm not mistaken), and the webserver itself must be able (just make sure that the encoding you are declaring is the same encoding as the using ). >

Other encodings other than UTF-8 can be used (Cp1252 / Windows-1252 or ISO-8859-1 / ISO-Latin or some other), but I do not recommend: UTF-8 is very universal, and should be understood by any non-prehistoric browser .

Finally, a comment on URLEncoder : what it does is to encode a string so that it can be used as a URL. That is not the same as encoding it to be used as content for an HTML page - if you use this method and then include the result on a page, the user will see the "strange" characters. If you need to hardcode your string in ASCII, you need to transform Unicode characters into HTML entities - which is a distinct process, and I do not know how to do it in Java. But at first this should not be necessary - and if it is, it should greatly increase the size of the page generated.

    
30.09.2015 / 19:21