Managing Character Encoding in an HTML-Client Application

In a localized BI Beans servlet application, character-encoding issues arise at two points:

Specifying the character encoding for a servlet request

The default QueryParameterProvider for the BI Beans is the ServletQueryParameterProvider. To specify the character encoding for servlet requests, pass the character encoding in the constructor for the ServletQueryParameterProvider.

Note: The URLEncoder is for transforming query parameter names, and it does not affect character encoding.

Setting the character set on the servlet response

To set the character set on the servlet response, call the setContentType of the response, as shown here:

response.setContentType("text/html;charset=" + charset);

Use the Internet Assigned Numbers Authority (IANA) codes as the charset. For a list of standard character sets, see http://www.iana.org/assignments/character-sets.

To ensure that all requests that are generated from the HTML page have the correct request encoding, you must add a <META> tag to the <HEAD> element of the HTML page as you generate it. Have the <META> tag specify the same character set as the response in the content type that you set.

The following example shows an appropriate <META> tag.

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

After you specify the character set in the servlet response, use the PrintWriter from the servlet response to instantiate the UIX ServletRenderingContext. The ServletRenderingContext automatically picks up the character encoding. The thin bean uses this character set to encode its HTML.

Note: You must specify the character set before you instantiate the ServletRenderingContext.

The following code shows how to specify character encoding for a request.


// be sure to set the character set before you instantiate // the rendering context response.setContentType("test/html;charset=" + charset); PrintWriter writer=response.getWriter(); ServletRenderingContext context = new ServletRenderingContext(this, request, response, writer); // Create a document that contains the thin beans DocumentBean document = new DocumentBean(); //Add style sheet information to the header HTMLWebBean head = new HTMLWebBean("head"); HTMLWebBean meta = new HTMLWebBean("meta"); meta.setHTMLAttributeValue("http-equiv", "Content-Type"); meta.setHTMLAttributeValue("content", "text/html"); meta.setHTMLAttributeValue("charset", charset); head.addIndexedChild(meta); // ... add more head elements -- code not provided in this example // then add the head to the document document.addIndexedChild(head); // ...add thin beans and other UIX beans to the document // ... code not provided in this example // then render the page, using the ServletRenderingContext document.render(context);