1

I have an utf-8 mysql DB.

In my jsp files, I use:

 <%@ page pageEncoding="UTF-8"%>
 ...
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />

My browser encoding is UTF-8.

In a form, I have an input text.

When I submit the form, if the value of the input text contains, for example:

à è é à ò ù €

it is inserted a "wrong" string in the db.

After the submit, If I print the input text content is already contains "strange" chars.

As I use whereever utf-8, I don't succeed in understanding what I have to change.

Sefran2
  • 3,578
  • 13
  • 71
  • 106
  • Maybe add some logging in the server code to check the input before sending it to the DB? – mthmulders Mar 28 '13 at 14:43
  • @mthmulders: it's already "wrong", that is it contains "strange" chars before it arrives to the db – Sefran2 Mar 28 '13 at 14:48
  • Then go back one step and use something like [Firebug](https://addons.mozilla.org/nl/firefox/addon/firebug/) or the Chrome Developer tools to check how the data are being sent to your server... – mthmulders Mar 28 '13 at 14:49
  • Look at this [backdated but wonderful response](http://stackoverflow.com/questions/138948/how-to-get-utf-8-working-in-java-webapps) – Ori Dar Mar 28 '13 at 15:00
  • @mthmulders: by using firebug, in the parameters section I see the right string (Testèòàùìé€), and in the source section there is "title=Test%C3%A8%C3%B2%C3%A0%C3%B9%C3%AC%C3%A9%E2%82%AC" – Sefran2 Mar 28 '13 at 15:02
  • That's also not the problem :) The `pageEncoding="UTF-8"` has already taken care of the HTTP response and browser encoding. As answered by orid, the problem is just most likely the HTTP request and DB connection encoding in server which is nowhere covered in your question. Also noted should be that the `` tag in question has totally no effect/influence in this all. It's only been used when you save the in browser retrieved HTML page to local disk file system and reopen it by `file://` URL instead of `http://` URL. – BalusC Mar 28 '13 at 15:15

1 Answers1

3

First, the fact that the DB encoding is UTF-8 is not enough, you should specify encoding at the driver connection as well by appending useEncoding=true&characterEncoding=UTF-8 to the JDBC URL

Second, the page encoding doesn't affects the way the browser encodes text. You should also take care for encoding at the request level: request.setCharacterEncoding("UTF-8")

Ori Dar
  • 18,687
  • 5
  • 58
  • 72
  • 1
    Please note that those JDBC URL parameters are specific to MySQL JDBC driver, not others. Please also note that setting request encoding this way only affects POST requests, not GET requests. See further also the hints/solutions listed in [Unicode - How to get the characters right?](http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html) – BalusC Mar 28 '13 at 14:59
  • This is true indeed. That is the case though – Ori Dar Mar 28 '13 at 15:14
  • @orid: I'm using tomcat and its connection pool. When I append `useEncoding=true&characterEncoding=UTF-8` to the driver connection I obtain an error (The reference to entity "useEncoding" must end with the ';' delimiter). By putting only `characterEncoding=UTF-8` and then changing the server.xml with URIEncoding="UTF-8" in the connector has no effect. – Sefran2 Mar 28 '13 at 15:26
  • @Criket: see the link I've attached just under your original post. It is a De-facto step by step tutorial. It also shows how to set encoding using Tomcat connection pool. It may have been that you haven't encoded the ampersand – Ori Dar Mar 28 '13 at 15:29
  • @orid: Thanks. Now the problem is with request.setCharacterEncoding("UTF-8"). I'll do some tests. – Sefran2 Mar 28 '13 at 16:09
  • @BalusC: I'm following your post [Unicode - How to get the characters right?](http://balusc.blogspot.it/2009/05/unicode-how-to-get-characters-right.html). Now I've an issue only with JSONObject encoding (I'm using org.json lib). The issue arises when I put the `àòùè쀀` in a JSONObject. I have `[{"values":"àòùèì\u20ac\u20ac"}]` instead of `[{"values":"àòùè쀀"}]`. Any suggetion? – Sefran2 Apr 08 '13 at 15:52