Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Problems rendering UTF-8

    Hi,

    I'm having a problem getting UTF-8 characters to render and was wondering if anyone has any ideas. Data has been verified as coming back from the backend in a valid UTF-8 format.

    I'm using a interceptor servlet that sets the character encoding and content type on the HttpServletResponse as follows:

    Code:
    resp.setCharacterEncoding("UTF-8");
    resp.setContentType("application/json; charset=utf-8");
    The body of the response is a String, which I have checked evaluates fine using UTF-8 via debugging using the following command:

    Code:
    new String(body.getBytes("utf-8"));
    I output from the interceptor servlet using the following command:

    Code:
    PrintWriter out = resp.getWriter();
    out.write(body);
    out.flush();
    out.close();
    And I have ensured the server is running using utf8 as the file encoding by adding the following argument to the server startup (both the Jetty development mode server, and the actual Jboss server we use for regular deployments):

    Code:
    -Dfile.encoding=utf8
    Finally, I have added the following lines to the jsp:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    ...
    ...
    <head>
    ...
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    ...
    </head>
    ...
    The stack trace I get in the developer console is as follows:

    Code:
    com.smartgwt.client.core.JsObject$SGWT_WARN: 09:02:50.875:XRP8:WARN:RestDataSource:appPersonRestDS:Error evaluating JSON: [object Error], JSON text: {"response": {
       "status": 0,
       "startRow": 0,
       "endRow": 1,
       "totalRows": 1,
       "data": [{...some data including UTF-8 characters...
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)
        at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
        at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
        at com.google.gwt.dev.shell.BrowserChannel.reactToMessages(BrowserChannel.java:1669)
        at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:401)
        at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
        at java.lang.Thread.run(Unknown Source)
    I have tried manually evaluating the JSON string (with UTF-8 characters in) using JSOHelper and it works fine.

    The screen renders fine if it only uses regular latin characters, but as soon as I try to display a record with non-latin characters, no data is displayed at all (only empty widgets).

    I'm using SmartGWT v2.2, IE7.

    If anyone can suggest any mistakes I have made with the current approach, or has any information about additional changes I need to make to run SmartGWT with UTF-8 (either config or code), it would be much appreciated.

    Thanks.

    Mike

    #2
    It looks like you've covered most of the bases, a few things to consider:

    1. it's not clear when in the lifecycle you're trying to set character encoding. If the response has been transmitted over the network already, you're too late.

    2. some other filter servlet could be involved and could be messing up your character encoding settings. Try disabling all filters so you know that your servlet is the only thing involved

    3. you've got a few different variations of "UTF8" (utf8, UTF-8, etc). Some might be incorrect and being ignored

    Comment


      #3
      Thanks - I've done some more checks now and the problem seems to be in the backend or on the server - I can get strings coming into the filter servlet that are UTF-8 encoded, but they need to be forcibly reminded that they are UTF-8 as opposed to ISO-8859-1:

      Code:
      body=new String(body.getBytes("ISO-8859-1"),"UTF-8");
      Once I've done this the string renders fine in SmartGWT, so it looks to me like I've either made a mistake in the HTTP headers in the backend, or have missed something in the server config. I shall investigate further.

      Thanks again...

      Comment

      Working...
      X