GlassFish 4 and UTF-8

Do you build web applications with JSF and GlassFish? And do you need to process inputs with characters which are coded with multi-bytes using UTF-8, e.g. German Umlauts? You might have recognized a strange behavior if you use GlassFish 4.

Now, let’s check for the problem by building a simple application. With NetBeans, simply choose New Project, Java Web, Web Application. Add JSF as framework. If you need detailed information how to create a web application with NetBeans, please take a look into my JSF tutorial.

This is the web page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Test German Umlaut</title>
    </h:head>
    <h:body>
        <h:form>
        <h:inputText value="#{test.text}"/>
        <h:commandButton action="#{test.noop}" value="Test"/>
        </h:form>
    </h:body>
</html>

And here the backing bean:

package web;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class Test {
    private String _text;

    public String getText() {
        return _text;
    }

    public void setText(String text) {
        _text = text;
    }

    public String noop(){
        return "";
    }

}

Not very much. Just an input field and a button to invoke an action, which displays the same page again. Now, start the app. Enter some multibyte chars, e.g. “äöü” and press the button. Using GlassFish 3 nothing seems to happen: Since the action returns an empty string, the same page with its data is re-displayed. This behaviour changes, if you use GlassFish 4: Every character (of multibyte chars) is replaced by two or more characters. Even though in the head of the page UTF-8 is declared, GlassFish treats the input as single-byte codes. The reason is GlassFish’s default encoding of iso-8859-1.

Solution:

Create a sun-web.xml or glassfish-xml file. Using Netbeans you may choose New, Other, GlassFish, GlassFish Descriptor. Add this line to change the default encoding:

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

That’s it.

 

 

This entry was posted in NetBeans, Tutorial, Web development and tagged , , . Bookmark the permalink.

2 Responses to GlassFish 4 and UTF-8

  1. Bruno at Janja says:

    This is not a solution, just a workaround.
    If you deploy on another server it will fail.

    • Michael Müller says:

      You’re right. I’m still seeking for a solution.
      Has anybody out there a hint for us?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>