On Fri, Feb 27, 2009 at 12:01 PM, Victor H De la Luz <[email protected]> wrote: > On Fri, Feb 27, 2009 at 4:32 AM, Andrej Konkow > <[email protected]> wrote: >> Hi Victor, >> >> this is no special JSF issue. But anyway. I had the same problem. You have >> to set the correct encoding before the first character is read from the >> stream. >> My solution was writing a filter which is put as the very first instance to >> handle the request. Example: >> >> public class CharacterEncodingFilter implements Filter >> { >> private String encoding; >> private FilterConfig filterConfig; >> >> /** >> * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) >> */ >> public void init(FilterConfig fc) throws ServletException >> { >> this.filterConfig = fc; >> this.encoding = filterConfig.getInitParameter("encoding"); >> } >> >> /** >> * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, >> javax.servlet.ServletResponse, javax.servlet.FilterChain) >> */ >> public void doFilter(ServletRequest req, ServletResponse resp, >> FilterChain chain) throws IOException, ServletException >> { >> // Before reading the first information we have to set the >> encoding to >> // utf-8. Otherwise the characterencoding is lost and special >> characters >> // as for example "€" are misinterpreted. >> req.setCharacterEncoding(encoding); >> >> chain.doFilter(req, resp); >> } >> >> /** >> * @see javax.servlet.Filter#destroy() >> */ >> public void destroy() >> { >> } >> } >> >> >> >> In your web.xml: >> <filter> >> <filter-name>CharacterEncodingFilter</filter-name> >> <filter-class>com.bla.CharacterEncodingFilter</filter-class> >> <init-param> >> <param-name>encoding</param-name> >> <param-value>UTF-8</param-value> >> </init-param> >> </filter> >> >> and the filtermapping as FIRST filtermapping: >> <!-- EncodingFilter is only needed together with request control filter. >> Otherwise >> the encoding is not correct --> >> <filter-mapping> >> <filter-name>CharacterEncodingFilter</filter-name> >> <url-pattern>/jsp/*</url-pattern> >> </filter-mapping> >> >> Hope it helps, >> >> Andrej >> >> -----Ursprüngliche Nachricht----- >> Von: Victor H De la Luz [mailto:[email protected]] >> Gesendet: Donnerstag, 26. Februar 2009 23:58 >> An: [email protected] >> Betreff: faces+mysql+tomcat and error in codification >> >> Hi! >> >> I have a problem very funny: >> >> I get my data from >> <h:inputText value="#{blogBean.blogPage.title}" /> >> >> I have in the header of my .xhtml like: >> <?xml version="1.0" encoding="utf-8"?> >> >> Now, I have Mysql 5.0.51a with >> >> Server characterset: utf8 >> Db characterset: utf8 >> Client characterset: utf8 >> Conn. characterset: utf8 >> >> and the collation of the table "blog" like >> utf8_unicode_ci >> >> I have the all the data into the database with utf8 codification. >> >> The problem is: >> >> When I get the data from the input text I get a wrong text >> with special characters, for example ó is substituted for ó. >> >> Then when I store the data in the database I write the ó character. >> >> The funny is that before I had this configuration: >> Server characterset: latin1 >> Db characterset: latin1 >> Client characterset: latin1 >> Conn. characterset: latin1 >> >> with the codification latin1 for all the database >> and utf-8 codification for the .xhtml. >> >> Now, with this configuration, when I get the data from the inputText >> the data is stored correctly in the String variable and in the >> database but when I >> get the data from the database this is displayed wrong >> (the same problem: ó is replaced by ó and etc when I get the data >> with title= rs.getString("title"); ) >> >> I tried to configure the connection to use explicit character encoding >> but this method fail, then I tried of get the data like >> >> byte[] stringValue = rs.getBytes("title"); >> String str = new String(stringValue,"utf-8"); >> >> but fail too, I changue the codification to latin1, ISO..., etc but >> always fails. >> >> In resume, the funny is that with utf8 codification in all the >> aplication the data is corrupted >> when I get the data from inputtext and if the codification is latin1 >> in the db and utf8 >> in the aplication then fails when I get the data from the database. >> >> Im using: Linux Debian Lenny, mysql 5.0.51a-24-log (Debian), >> myfaces-impl-1.2.5.jar >> and mysql-connector-java-5.1.5-bin.jar. >> >> Thanks in advanced! >> >> -- >> ItZtLi >> > > > Thanks! > > But, if the utf8 is the explicit encoding in the .xhtml, > ¿Why we need set the encoding again in the request in the server side? > and the most important question: > ¿Why the DB configuration influences in the codification of the stream > from inputText? > > > -- > ItZtLi >
Now I understand! The problem is in the POST encoding, I get the data always in latin1 codification. When I get the data in the String variable in Java, the translation is wrong because the request have wrong codification (latin1) and I have the configuration for UTF8. So, the variable have wrongs characters. When I have the latin1 codification in the db, Tomcat makes the right translation in the variables, the data is stored in the right way, but when I get the data from the db then exists the same problem, but now from latin1 to utf8... I think is a bug of tomcat6, like Andrej said... -- ItZtLi

