JSF Encode UTF - 8?
Now I am working with my friend, he is Vietnamese and he wants to create a website using the Vietnamese Language, but we have a problem with Encode UTF-8. I wrote a Filter class as follows:
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharsetFilter implements Filter
{
private String encoding;
public void init(FilterConfig config) throws ServletException
{
encoding = config.getInitParameter("requestEncoding");
if( encoding==null ) encoding="UTF-8";
}
public v开发者_StackOverflow社区oid doFilter(ServletRequest request, ServletResponse response, FilterChain next) throws IOException, ServletException
{
// Respect the client-specified character encoding
// (see HTTP specification section 3.4.1)
if(null == request.getCharacterEncoding())
request.setCharacterEncoding(encoding);
/** * Set the default response content type and encoding */
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
next.doFilter(request, response);
}
@Override
public void destroy() {}
}
config in web.xml:
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.mypackage.CharsetFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
in Server Resources - sun-resources.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="vietweb" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="3306"/>
<property name="databaseName" value="vietweb"/>
<property name="User" value="root"/>
<property name="Password" value="12345"/>
<property name="useUnicode" value="true"/>
<property name="characterEncoding" value="UTF8"/>
<property name="URL" value="jdbc:mysql://localhost:3306/vietweb"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="jdbc/vietweb" object-type="user" pool-name="mysql_vietweb_rootPool"/>
</resources>
in JSF page:
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
But it is not being encoded to Vietnamese. How can we make it encode to Vietnamese?
Problem with save data into DB ( i use DB is MySQL) I want/expect this output:
chuyển phát nhanh để chuyển tới những cuốn sách
but I see this unexpected output:
chuy?n phát nhanh ?? chuy?n t?i nh?ng ??u sách
The symptoms indicate that the MySQL DB and/or table is been created as ISO-8859-x instead of UTF-8. Any character which is been outside the ISO-8859-x range will then be stored as ?
. Update the DB and/or table as follows:
ALTER DATABASE dbname DEFAULT CHARACTER SET utf8;
USE dbname;
ALTER TABLE tblname CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
The webapp and DB connection settings are perfectly fine. You would otherwise have seen mojibake instead of question marks.
That said, the CharsetFilter
is in essence unnecessary. JSF on Facelets already uses UTF-8 by default. When you're still using legacy JSP instead of Facelets, all you need to do is adding the following line to top of JSP:
<%@ page pageEncoding="UTF-8" %>
and do only request.setCharacterEncoding()
in the filter (and do nothing with response
).
See also:
- Unicode - How to get characters right?
I've solve my problem by this statement:
jdbc:mysql://localhost:3306/db_name?**useUnicode=yes&characterEncoding=UTF-8**
精彩评论