views:

86

answers:

1

Hello All , now i work with my friend , he is Vietnamese and he want create website with Vietnamese Language, but we have problem with Encode UTF 8 i was write class Filter follow:

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 void 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"&gt;
<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 can not encode to Vietnamese How can we encode to Vietnamse ? Thank you

Problem with save data into DB ( i use DB is MySQL) i want expect output

chuyển phát nhanh để chuyển tới những cuốn sách

but unexpect output is:

chuy?n phát nhanh ?? chuy?n t?i nh?ng ??u sách 
+2  A: 

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:

BalusC
Many Thank You Balus, i have small question is how can i display HTML tag inside h:outputText because i save into DB is plain text attach HTML and i want display it on JSF page. Thank You!
MYE
Add `escape="false"`. Keep XSS risks in mind! See also [this answer](http://stackoverflow.com/questions/3949295/how-to-display-rich-content-using-jsf-component/3949793#3949793) which I posted yesterday.
BalusC
Thank you, i also find it too :D
MYE
Sorry for stupid question Balus, Why it return ?? after re-deploy application on server?
MYE
You have to fix the malformed DB content yet.
BalusC
MYE
Hi BalusC i try many way to solved problem Encoding with Glassfish but many way can not solved this problem, do you have ideas for solve it? Thankyou
MYE
You told that the DB contains malformed data. Did you fix it? Update the DB and table to use UTF-8, then delete data and re-insert correct data. Check again if insertion went right.
BalusC
Hi BalusC i was check in my DB, it work well, i think my problem in Glassfish, when it deploy Vietnamese can encode but when it re-deploy it occur unexpected character
MYE
You have to be more specific about where the problem occurs.
BalusC
In Database i inserted some vietnamese character it Database with WorkBench Management, it good, and deploy good, i found out problem occour with form, if i donn't change any thing in database when it load to form when server deploy, i can see vietnamese character but when i edit some data on form and save it into Database, it occur unexpected character
MYE
Which "unexpected character" exactly? Question marks or mojibake?
BalusC
yes unexpected characters are Question Marks
MYE
Today i sovled my problem
MYE