3

For the first time, I'm creating a simple website with JSP, Servlets, a Tomcat server, MySQL and Netbeans (if that helps).

Thanks to JPA EntityManager, I can insert data into my database like this :

    EntityManager entityManager = Persistence.createEntityManagerFactory("test").createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(usr);
    entityManager.getTransaction().commit();

Where usr is an Entity Class User generated thanks to Netbeans. Everything works perfectly except the fact that it doesn't support characters such as accents.

Assuming I want to insert the word "cliché" :

  • If I try to print the value from my User class, everything is fine.
  • When the value is sent, if I try to SELECT the value from my base, I've got strange characters instead of the "é"

Database fields are utf8_unicode_ci. If I try to make a simple INSERT from MySQL prompt such as

INSERT INTO table (name) VALUES("ééé");

it's working perfectly.

As I read on some websites, I modified my persistence.xml with the following line :

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8&amp;zeroDateTimeBehavior=convertToNull"/>

but the result is the same.

EDIT : Here are the properties of my User Class

@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByIdUsers", query = "SELECT u FROM Users u WHERE u.idUsers = :idUsers"),
    @NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
    @NamedQuery(name = "Users.findByPasswd", query = "SELECT u FROM Users u WHERE u.passwd = :passwd"),
    @NamedQuery(name = "Users.findByFirstName", query = "SELECT u FROM Users u WHERE u.firstName = :firstName"),
    @NamedQuery(name = "Users.findByLastName", query = "SELECT u FROM Users u WHERE u.lastName = :lastName"),
    @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
    @NamedQuery(name = "Users.findByEmailAndPwd", query = "SELECT u FROM Users u WHERE u.email = :email AND u.passwd = :passwd")})
public class Users implements Serializable {
    @Basic(optional = false)
    @Column(name = "address")
    private String address;
    @Basic(optional = false)
    @Column(name = "city")
    private String city;
    @Basic(optional = false)
    @Column(name = "postal")
    private String postal;
    @Basic(optional = false)
    @Column(name = "phone")
    private String phone;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idUsers")
    private Integer idUsers;
    @Basic(optional = false)
    @Column(name = "username")
    private String username;
    @Basic(optional = false)
    @Column(name = "passwd")
    private String passwd;
    @Basic(optional = false)
    @Column(name = "firstName")
    private String firstName;
    @Basic(optional = false)
    @Column(name = "lastName")
    private String lastName;
    @Basic(optional = false)
    @Column(name = "email")
    private String email;

As I read on the web, I also tried to modify my catalina.bat by adding the following lines but it didn't change anything.

set JAVA_OPTS=%JAVA_OPTS% -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8

Any help is much appreciated.

Neeko
  • 1,139
  • 1
  • 10
  • 26
  • In Glassfish I use this on WEB-INF-->glassfish-web.xml Maybe it´s similar in tomcat – Goldbones Dec 24 '14 at 11:37
  • I couldn't find a similar property for Tomcat – Neeko Dec 25 '14 at 16:04
  • I don't see anything wrong with your code, so I'll take some long shots: What version of MySQL are you using? What version of the MySQL JDBC driver are you using? Exactly what strange characters do you see? (By any chance are you seeing `cliché`?) – VGR Dec 25 '14 at 23:16
  • @VGR My MySQL version is 5.6 (the last one). For the driver, the version is 5.1.34. And as you said, the characters are "é" for "é" – Neeko Dec 25 '14 at 23:30
  • 1
    "é" is a Latin-1 interpretation of the two UTF-8 bytes which represent "é". This has me wondering if perhaps your column and/or table is actually using Latin-1 (a.k.a. ISO 8859-1). Does a statement that uses non-Latin-1 characters work, such as `INSERT INTO table (name) VALUES("↑↑↑");`? – VGR Dec 26 '14 at 01:13
  • Yes, it actually works.. I just read your previous answer here http://stackoverflow.com/questions/16527576/httpservletrequest-utf-8-encoding/16549329#16549329 and it fixed my problem. I don't really get what was the issue. I made sure that all my columns and tables are using UTF-8. As you helped me a lot, I let you add the answer so that I can accept it ! Thanks a lot for your help ! – Neeko Dec 26 '14 at 01:38

3 Answers3

2

I was able to fix my problem thanks to VGR's answer here : HttpServletRequest UTF-8 Encoding

Thanks a lot for your help.

Community
  • 1
  • 1
Neeko
  • 1,139
  • 1
  • 10
  • 26
0

try add this config in your persistence.xml

<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.charSet">UTF-8</property> 
Rafael Zeffa
  • 2,334
  • 22
  • 20
0

You can add unicode encoding filter to web.xml:

  <filter>
    <filter-name>CharsetFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
       <param-name>forceEncoding</param-name>
       <param-value>true</param-value>
     </init-param>
  </filter>

if you use Spring you can use 'org.springframework.web.filter.CharacterEncodingFilter' class to encode all requests, else you should use other encoding class.

Babak Behzadi
  • 1,236
  • 2
  • 16
  • 33