How can I specify the encoding of HTTP request parameters?

GlassFish treats the encoding of a request's URI separately from the encoding of its parameters. In the case of a GET request, query parameters are appended to the URI (separated by <code>?</code>), whereas in the case of a POST request, they are carried inside the request body.

The two types of encoding are specified in different places:

  • The URI encoding may be specified as the value of a property named <code>uriEncoding</code> of the <code><http-listener></code> element in <code>domain.xml</code>. This encoding, which defaults to <code>UTF-8</code>, is applied to any request URI received by the corresponding HTTP listener.
  • The request parameter encoding, on the other hand, defaults to <code>ISO-8859-1</code> and may be overridden in one of two places: Either programmatically inside a servlet, by calling the ServletRequest's <code>setCharacterEncoding</code> method, or declaratively, by specifying the parameter encoding as the value of the <code>default-charset</code> attribute of the <code><parameter-encoding></code> element in <code>sun-web.xml</code>, as shown in the following example, which sets the request's parameter encoding to <code>UTF-8</code>:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Sun ONE Application Server 8.0 Servlet 2.4//EN" "http://www.sun.com/software/sunone/appserver/dtds/sun-web-app_2_4-0.dtd">

<sun-web-app>
  <locale-charset-info default-locale="">
    <locale-charset-map locale="" charset=""/>
    <parameter-encoding default-charset="UTF-8"/>
  </locale-charset-info>
</sun-web-app>

See also this FAQ entry.