users@glassfish.java.net

Request parsed with wrong character encoding

From: Vetle Roeim <vetler_at_gmail.com>
Date: Thu, 18 Nov 2010 11:38:25 +0100

Hi,

Recently, our web application started having character encoding
problems. After a little bit of debugging, I found out that this was
caused by one of our servlet filters, which caused the request to be
parsed as ISO-8859 instead of UTF-8. We use UTF-8 "everywhere", i.e.
in XML declaration, in the meta-element, etc.

The filter that caused problems, had the following doFilter method:

@Override
public void doFilter(final ServletRequest arg0, final ServletResponse
arg1, final FilterChain arg2) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest)arg0;
    if (req.getMethod().equals("POST")) {
        String tmp = null;
        for (String name :
(List<String>)Collections.list(req.getParameterNames())) {
            if (name.endsWith("someString")) {
                tmp = req.getParameter(name);
            }
        }

        if (tmp == null {
            // Something
        }

    }

    arg2.doFilter(arg0, arg1);
}

I assume that trying to get the parameters triggers parsing the
request... If we set the character encoding explicitly, immediately
after the cast to HttpServletRequest with
req.setCharacterEncoding("UTF-8"), everything works fine.
If we don't set it, req.getCharacterEncoding() returns null, and it is
parsed as ISO-8859.

We have a bunch of filters, but I tracked down the problem here. I
also tried setting default character set in sun-web.xml, but there was
no change:

<locale-charset-info>
    <parameter-encoding>
        <default-charset>UTF-8</default-charset>
    </parameter-encoding>
</locale-charset-info>

Any ideas why this filter would cause the request to be parsed as
ISO-8859 instead of UTF-8, and what we can do to fix it?

Using Glassfish 3.0.1, by the way.

Regards,
Vetle