How can I use multibyte data in FORM submissions? By default, when a FORM is submitted, its data will be encoded using the ISO-8859-1 character encoding. This article explains what to do if the FORM's data rely on a multibyte encoding and therefore cannot be expressed using ISO-8859-1. Some background information: A FORM's producer (server-side web application) is responsible for indicating to the client the FORM's character encoding, via the <code>charset</code> component of the <code>Content-Type</code> response header or the corresponding <code>HTTP-EQUIV</code> tag. The browser will use the same character encoding when submitting the data from the FORM, but unfortunately, most browers will omit the character encoding from the <code>Content-Type</code> request header, leaving the server guessing at what has been sent. This is where hidden FORM fields come into the picture: The application knows the character encoding of the FORM, specifies it in the response (using one of the mechanisms mentioned above), and also specifies it as the value of a hidden FORM field embedded in the FORM itself. This approach relies on the fact that the browser will use the same encoding (determined from the response containing the FORM) when submitting the data from the FORM, so all that the application has to do is instruct the server to use the value of the hidden FORM field as the character encoding of the FORM's data, once the FORM has been submitted. The following example shows the use of a hidden FORM field in a login page used by FORM-based authentication. The hidden FORM field is named <code>my_form_encoding</code> and specifies UTF-8 as the character encoding of the FORM's login parameters.
UserName: <INPUT TYPE="text" NAME="j_username" VALUE=""> <BR>
Password: <INPUT TYPE="password" NAME="j_password" VALUE=""> <BR>
<INPUT TYPE="hidden" NAME="my_form_encoding" VALUE="UTF-8">
The application will instruct the server to look for this hidden FORM field and use its value as the character encoding of the FORM's data, by bundling a <code>sun-web.xml</code> deployment descriptor with the following contents:
<sun-web-app>
<locale-charset-info>
<parameter-encoding form-hint-field="my_form_encoding"/>
</locale-charset-info>
</sun-web-app>
Note how the value of the <code>form-hint-field</code> attribute matches the name of the hidden FORM field. This approach is specific to GlassFish and will not work with other application servers.
|