WebObjects 5: How to Specify the Character Encoding for a Web Browser

A WebObjects application may set the character encoding used by the client Web browser so that international characters are displayed correctly.
Character encodings can be set at various places in a WebObjects application, such as string encoding for session data, XML encoding, database server encoding, or the encoding used to communicate with the client Web browser. This document only discusses the latter.

It is important to select the proper character encoding and initialize WebObjects accordingly, so that the characters of your chosen language are displayed correctly. This document discusses using Japanese characters, but this information can be used for other languages as well.

Setting the encoding inside WebObjects

UTF-8, which is a way to represent Unicode, covers a wide range of Japanese characters and allows Japanese text to be mixed with other languages (European, Asian, and so forth). Therefore, this encoding is preferred compared to other Japanese-only encodings such as Shift-JIS.

In your Application.java file, override two methods, takeValuesFromRequest() and appendToResponse() to specify the proper encoding (UTF-8 is used as an example here). The method appendToResponse() also sets the proper encoding in each WOResponse header.
      public void takeValuesFromRequest(WORequest r, WOContext c) {
        r.setDefaultFormValueEncoding("UTF8");
        super.takeValuesFromRequest(r,c);
    }

    public void appendToResponse(WOResponse r, WOContext c) {
        r.setContentEncoding("UTF8");
        super.appendToResponse(r,c);
        r.setHeader("text/html;charset=utf-8", "Content-Type");
    }

See Sun's encoding documentation (http://java.sun.com/j2se/1.3/docs/guide/intl/encoding.doc.html) to determine the correct encoding for your environment.

Example: Resolving a browser display problem when using Shift_JIS (JIS X0208:1997)
Problem description: Web browser does not display correctly the vendor specific characters
Encoding used: Shift_JIS

Solution:

1. Windows-31J is the correct character set for displaying the vendor specific characters.

2. Modify the character encoding to "MS932", Windows Japanese.

3. The request header should be set to ("text/html;charset=Windows-31J", "Content-Type")

Code example:
public void takeValuesFromRequest(WORequest r, WOContext c) {
        r.setDefaultFormValueEncoding("MS932");
        super.takeValuesFromRequest(r,c);
    }

    public void appendToResponse(WOResponse r, WOContext c) {
        r.setContentEncoding("MS932");
        super.appendToResponse(r,c);
        r.setHeader("text/html;charset=Windows-31J", "Content-Type");
    }


For more information, see:

Microsoft Windows Codepage : 932 (Japanese Shift-JIS)
<http://www.microsoft.com/globaldev/reference/dbcs/932.htm?&gssnb=1>

Mapping Differences Between JIS X 0221 and Code Page 932
<http://support.microsoft.com/default.aspx?kbid=286776>

Windows31-J and other character sets
<http://www.iana.org/assignments/character-sets>

Important: On Mac OS X, Internet Explorer may not display the Japanese characters correctly even with the MS932 encoding. Use Safari 1.0 or later instead.

Note: Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. Apple assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. Apple provides this only as a convenience to our users. Apple has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are dangers inherent in the use of any information or products found on the Internet, and Apple assumes no responsibility in this regard. Please understand that a third-party site is independent from Apple and that Apple has no control over the content on that website.

Document 17159: "Locating Vendor Information" can help you search for a particular vendor's address and phone number.
Published Date: Oct 7, 2016