1 Reply Latest reply on Feb 4, 2009 5:07 PM by - -

    Getting a JSF webapp to use UTF-8

    - - Newbie

      Hi together. Hope someone can help me out, I feel a bit confused ;)

      I am developing a JSF webapp using facelets and Richfaces, deploying it on a JBoss 4 AppServer and I am using MySQL database.

      So far so good. Everything works fine except some bugs with switching to UTF-8. I am getting misinterpreted signs instead of äöü (german ae, oe, ue. and what you see right here, are the misinterpreted signs even in this forum. seems phpbb isnt utf-8, too ^^). So the first thing was googling. And I googled... and tried...and googled. What I found out so far is, that there are so many different ways to tell the webapp to use UTF-8, its really curious. And.... not a single one works for me :(

      1st possibility:

      There is a tag inside every .xhtml page, which can be extended through lang="de" (or maybe lang="de_DE"?):

      <HTML lang="de">


      My question here: Why should I? How does it affect a webapp (in fact in my case it doesnt)

      2nd possibility:
      Inside the header of every .xhtml page there should be a first entry like this:
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>


      This already seems to sound right. And trying Firefox -> View -> Page Encoding shows me UTF-8 as marked for my .xhtml files. So far so good.

      3rd possibility:
      I am using windows, which doesnt use UTF-8 as default. Inside Eclipse I found out that you have to pay attention while saving files (properties, .xhtml...). Right Mouse -> Properties shows you wether you saved your files in UTF-8 or still in system default. After switching all my JSF project files to UTF-8 (I didnt switch all my EJB3 project files to UTF-8) I had to redo some text and change to german ae, oe, ue

      4th possibility:
      This leads us away from the .xhtml files and lets us change our web.xml by adding

       <filter>
       <filter-name>UTF8 Filter</filter-name>
       <filter-class>myJSFproject.filter.UTF8Filter</filter-class>
       </filter>
       <filter-mapping>
       <filter-name>UTF8 Filter</filter-name>
       <url-pattern>*.xhtml</url-pattern>
       </filter-mapping>
      


      and developing a small java class like following:

      package myJSFwebapp.filter;
      
      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 UTF8Filter implements Filter {
      
       public void destroy() {}
      
       /**
       * @param ServletRequest
       * @param ServletResponse
       * @param FilterChain
       */
       public void doFilter( ServletRequest request,
       ServletResponse response,
       FilterChain chain ) throws IOException,
       ServletException {
       request.setCharacterEncoding("UTF-8");
       response.setContentType("application/xhtml+xml");
       chain.doFilter(request, response);
       }
      
       public void init(FilterConfig arg0) throws ServletException {}
      
      }
      


      This one should help changing the request and response to UTF-8... but it actually doesnt. Or thats not enough.

      5th possibility:
      The database tables must use UTF-8 too. So be sure to have your database schema and tables configured right.

      Unfortunately I did. But my problem isnt text from the database (which seems to get me correct german signs!). My problem is reading text from a .properties file which gets shown wrong. So I went on googling...

      6th possibility:
      The forms can be changed to submit UTF-8 too. Whyever because the request / response should already be changed to UTF-8 through position 4, see above. It would work like:

      <form method='post' accept-charset="UTF-8" ...>


      And if you dont want every single form to change you can even try this by extendig web.xml once again:

       <!-- http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/ -->
       <!-- Now all forms take only UTF-8 -->
       <context-param>
       <param-name>PARAMETER_ENCODING</param-name>
       <param-value>UTF-8</param-value>
       </context-param>
      


      Sounds great, but is it really neccesary? And it doesnt help me out because my text is coming from a properties file, not from a submitted inputed text.

      7th possibility:
      You can even change your f:view tag like following:

      <!--How do I change the response contentType?-->
      <f:view contentType="application/xhtml+xml"> ... </f:view>


      It just ...wont help anyways.

      and the last and in my opinion really cruelest way would be...

      8th possibility:

      Set JAVA_OPTS="$JAVA_OPTS -Djavax.servlet.request.encoding=UTF-8 -Djavax.servlet.response.encoding=UTF-8 -Dfile.encoding=UTF-8" in start script (run.sh....) from JBOSS.

      To be honest... I didnt even try this one. Cant be right, can it??

      By the way, you can even change the console output for JBOSS to UTF-8 in Eclipse server view, but that doesnt matter.

      So finally I googled, I tried... and found NO way of using UTF-8 :( I really need help with this one and hope, anyone knows a bit more about this and can help me out! Would be great! Thanks in advance! Let me her what you tried and what you know about the above solutions.


        • 1. Re: Getting a JSF webapp to use UTF-8
          - - Newbie

          seems I know a bit more than before...

          i am developing under windows in german. with german signs. but to be compatible with linux and other developers it seems like I have to save my java files in UTF-8. and to get the pages shown in the browser in UTF-8 you should save it in UTF-8, too.

          but the most important part, the properties files with your text, mustnt be saved in utf-8. they stay in ISO-8.... format. then it works.

          can anyone tell me why?? how can a iso property file help me with foreign languages? does this make sense?