I have a MySql db that uses UTF-8 Unicode charset. In my JSP when I have:

<meta http-equiv="Content-Type" content="text/html" charset="ISO-8859-1">

It'll insert correctly the spanish accents in the db. However when I use:

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

It'll insert García instead of García

Do you guys know why with UTF-8 it won't insert the accents?

有帮助吗?

解决方案 2

Since I am using Spring, I solved my problem using the CharacterEncodingFilter. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form

其他提示

I don't know about JSP, but this frequently is related to how the db connection reads the data from the database. In PHP, to ensure you are reading a table (or database) with utf-8 encoding, you should declare the character set of the connection:

if(!$conexion->set_charset("utf8")) {
$errsql['juego_de_caracteres']="Error al conectar al servidor mysql :    ".mysqli_connect_error();

So I guess you can do something similar in Java. You can read about character encoding in the web. This link: http://www.joelonsoftware.com/articles/Unicode.html I found useful.
Also, if your settings are not correct, its rather unpredictable what the output might be, because I've seen sometimes in Windows characters displaying right with ISO-8859-1 but they will surely not show right in all systems.

There are three things you have to set

(1) tell the servlet engine what the character encoding was used in the source files

(2) tell the servlet engine what character set you want to be output

(3) tell the receiver what character encoding the stream is in.

You meta tag accomplishes only the last of these, but you may not actually be sending an output stream formatted in that encoding.

Use this to tell the servlet engine how to read the source file, and you need to put in the actual character set that the source file is encoded in. It will be UTF-8 only if you have used a UTF-8 editor. It is probably ISO-8859-1 if you didn't take some pains to make it UTF-8:

<%@page contentType="text/html;charset=???????"%>

And use this to tell it how to output the stream:

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

In your case, since you know the data in the DB is encoded in UTF-8, it is probably this last setting that will solve the problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top