mySQL - UTF-8 for JSP and Tomcat

How to display UTF-8 characters in Tomcat using MySQL as the data source!

1. Make sure your MySQL database is using the UTF-8 character set.

2.Tell the JDBC connector that it has to talk to the database using UTF-8.
To do this add useUnicode=true&characterEncoding=UTF-8 to the connection URL in the JDBC configuration. Your connection URL will look something like this: jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=UTF-8.

3. Make sure the browser knows that what it's receiving is actually UTF-8.
In your JSP files add <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>at the top.
In your Servlets, make sure the right HTTP headers are sent back to the client by adding the line response.setContentType("text/html;charset=UTF-8");.
Of course, you'll have to use another MIME type than text/html if you're not going to display HTML.

4. Tell Java that you're using UTF-8 by configuring the Java options.
Add the parameter -Dfile.encoding=UTF-8 to your Java options, either in the catalina.bat file or by clicking on the Java tab in the Monitor Tomcat program.

5. if you're using Struts to handle web forms - is to make sure all input from the client is converted to UTF-8.
This is done with a little bit of coding and a configuration change

http://www.vegard.net/archives/bg000815.php

No comments:

Post a Comment