Reprint from: http://hermanland.com/a/1/2008/04/01/1207062060000.html
UTF-8 encoding can be used to present multilingual web pages. Java handles all string internally as Unicode 16 and expects all input or output data encoded in ASCII. Therefore, we need to explicitly tell Java that our data are encoded in UTF-8. Here are the steps for Tomcat + JSP + MySQL.
-
Tells the JSP compiler that the JSP page is encoded in utf-8. The JSP page directive to do this is:
<%@page pageEncoding="UTF-8"%>
-
Tells the JSP compiler to sent data to the browser in utf-8 encoding. The JSP page directive to do this is:
<%@ page contentType="text/html; charset=utf-8"
language="java" import="java.sql.*" errorPage="" %>The directive also sets the HTTP Content-Type header accordingly.
-
As a good practice, markup the html page with a META tag to indicate the data is utf-8 encoded.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
Tells JDBC that the data in MySQL database is encoded in utf-8. The attributes for JSTL SQL tag to do this are:
<sqla:setDataSource
var="example"
scope="application"
driver="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost:3306/test?
autoReconnect=true&useUnicode=true&characterEncoding=utf-8" user="??"
password="??"/> -
Tells the ServletRequest object that the data from the input html FORM is utf-8 encoded. This should be done before reading any data from the object. The scriplet to done this is:
<% request.setCharacterEncoding("utf-8"); %>
- If you are using Notepad to edit the JSP, make sure to save the page as utf-8 type instead of the default ascii. Some html editors are clever enough to do it automatically by inspecting the HTML META tag.