March 2, 2014

Arabic (UTF-8) Characters encoding Issue in JSF applications

Problem:

Some times when you develop a web application using JSF you face issue posting the Arabic characters to application server with different encoding.

Solution:
Create a filter for POST requests that add character encoding to the post request as follow:

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException   {
     request.setCharacterEncoding("UTF-8");
     chain.doFilter(request, response);
  }

  @override
  public void destroy() {
  }

  @Override
  public void init(FilterConfig arg0) throws ServletException {
  }
}

And for the GET requests you need to update the application server's deployment descriptor.
In glass fish as example, create glassfish-web.xml file under WEB-INF folder and add the following line:


<parameter-encoding default-charset="UTF-8">

No comments: