Friday, April 27, 2012

JBoss and UTF-8 character encoding

Often you might face problems with character encoding, either in page content, form data or URL params. Data stored to or retrieved from a database doesn't show correct characters, submitted form data and query parameters are shown incorrectly.

Usually it's recommended to use UTF-8 encoding wherever possible. Here's how to set up JBoss so that it really really uses UTF-8 everywhere.

First of all, in your JSP/JSF pages set the HTTPRequest encoding as follows:

<%
   request.setCharacterEncoding("UTF-8");
%>

I usually also set the encoding via a JSP tag just to make sure:

<%@page contentType="text/html; charset=UTF-8"%>

Next, add a META tag to your HTML so that the browser (and search engines) also know your encoding:

<head>
   <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>

Is that it ? No. JBoss encodes URL parameters with ISO-8859-1, so you need to tune the server.xml file located under /deploy/jboss-web.sar/. To the <connector> element (for either HTTP or AJP or both) you need to add URIEncoding="UTF-8".

Lastly, if you're using a database such as PostgreSQL, remember to create your database with UTF-8 encoding.


No comments:

Post a Comment