1

I am having a problem when trying to input and save a Chinese string into MySQL via JSF and Hibernate.

Actually, I used "System.out.print" and detected garbled words happened, after typing 我(me) in JSF input field but before saving to database. Here is part of the code: index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:panelGrid columns="2">
                <h:outputText value="Input"/>
                <h:inputText value="#{showBean.input}" />
            </h:panelGrid>
            <h:commandLink action="#{showBean.show()}" value="Show"/>

        </h:form>
    </h:body>
</html>

ShowBean.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class showBean {
    String input;
    public showBean() {
        input = null;
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        System.out.println("set input " + input);
        this.input = input;
    }

    public String show(){
        System.out.println("show input " + input);
        return "";
    }
}

The console output is : set input ???è??è?????è??è????? show input ???è??è?????è??è?????

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Hi, I don't think people will be able to help you without showing some code. How are you inserting the data into the database? – Pekka Dec 18 '12 at 14:18
  • 1
    You need to be more specific at which step exactly the character encoding fails. Is it garbled *before* saving in the DB? If not, is it garbled *while* saving in the DB? (thus, when you look in the DB by some admin tool). Etc. – BalusC Dec 18 '12 at 14:41
  • Okay, thank you for the update. So, the HTTP request body encoding is wrong. First this, are you using JSP or Facelets as view technology? If Facelets, are you using a 3rd party component library like PrimeFaces? If so, are you sending a normal or an ajax request? – BalusC Dec 18 '12 at 19:26
  • I just use normal facelets, and I didn't use primefaces here, and sending a normal request. I want to see what I get from the input box, but it is garble after submitting the form – user1912972 Dec 18 '12 at 19:47
  • Okay, then the cause is not Facelets as it uses by default UTF-8. I'll post an answer. – BalusC Dec 18 '12 at 20:21

2 Answers2

0

Provided that you're indeed using Facelets (which uses UTF-8 by default) and are not using PrimeFaces ajax (which is known to messup request body encoding), your problem has 2 causes:

  1. MySQL JDBC driver character encoding is not been set to UTF-8. This caused garbled characters in the DB.

  2. Eclipse console character encoding is not been set to UTF-8. This caused garbled characters in System.out.

The solutions are:

  1. Add useUnicode=yes and characterEncoding=UTF-8 parameters to the JDBC connection. You can specify it either as query string in the JDBC URL

    jdbc:mysql://hostname:3306/db_name?useUnicode=yes&characterEncoding=UTF-8
    

    or as connection properties in JDBC datasource, exactly the same way as you specified username, password, etc.

  2. Tell Eclipse to use UTF-8 as console encoding by Window > Preferences > General > Workspace > Text File Encoding:

    enter image description here

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I really appreciate your detailed answer. – user1912972 Dec 19 '12 at 06:07
  • I tried your way and other similar solutions many times, BUT I identified the problem is not about the MYSQL and Hibernate, but about the JSF input box. Sorry about the confusion! As you have seen the code I provided above, this is a new and simple Netbean project I created to identify the problem, and it doesn't has database connection. At last, the console show garbed words after I typed a chinese word and clicked "show" in index. How can I fix this input problem? – user1912972 Dec 19 '12 at 06:18
0

If you use glassfish just add the following code to glassfish-web.xml

<glassfish-web-app>
   <parameter-encoding default-charset="UTF-8" />
</glassfish-web-app>