webtier@glassfish.java.net

Ajax partial response encoding issue

From: <webtier_at_javadesktop.org>
Date: Thu, 21 Jan 2010 01:05:53 PST

During response rendering ajax request performs rendering by using the encoding from the request. However, the encoding in the partial response document is hardcoded to UTF-8 which leads to problems at least with certain browsers (e.g. IE). Lets assume the request comes in with ISO-8859-1 and thus the response with current code looks like this:

<?xml version="1.0" encoding="utf-8"?>
<partial-response>...</partial-response>

If I have content with for example scandinavian alphabets (åäö), the response document causes problems in the browser: HTTP headers state that encoding is ISO-8859-1, the XML document states encoding is UTF-8 but the content is encoded with ISO-8859-1. This clearly violates encoding rules. I made a little change to PartialResponseWriter which seem to work pretty nicely and fixes this problem.

Current code:

    public void startDocument() throws IOException {
        ResponseWriter writer = getWrapped();
          writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        writer.startElement("partial-response", null);
    }

And the new code looks like this:

    public void startDocument() throws IOException {
        ResponseWriter writer = getWrapped();
          // PACI 20.1.2010 {
          String encoding = writer.getCharacterEncoding( );
          if( encoding == null )
                encoding = "utf-8";
          writer.write("<?xml version='1.0' encoding='" + encoding + "'?>\n");
          // } PACI 20.1.2010
        writer.startElement("partial-response", null);
    }

Cheers,

Paci
[Message sent by forum member 'taikaviitta' (pasi.salminen_at_tapiola.fi)]

http://forums.java.net/jive/thread.jspa?messageID=382104