WalterP
WalterP

Reputation: 41

UTF-8 Encoding doesn't work with Errai and Tomcat

we are developing an web application using GWT 2.4, Errai 1.3.2. It is running on Tomcat 6 (6.0.35) and built by Maven (3.0.4).

When running this application on Tomcat, the transfer of special cases is not working. More specific, the request works fine but the response of special characters converts them to �. When using the errai maven archetype, it has the same behaviour. When using GWT-RPC instead of errai RPC, everything works fine. Running the same application in Dev-Mode, the problem doesn't occur.

When looking at the request/response in chrome, both have character encoding UTF-8.

I think this might be an errai bug because there is some String encoding in errai before sending the response.

It would be great, if someone could help me!! It's really a tricky problem...

Thanks, Walter


PS: I have already tried the following potential solutions, which all do not work:

Setting index.html head:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

Define a custom Servlet Filter

WEB.xml

<filter>
    <filter-name>SessionFilter</filter-name>
    <filter-class>at.apa.excelsa.web.server.SessionFilter</filter-class>
    <init-param>
        <param-name>requestEncoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SessionFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

Filter.java

public class SessionFilter implements Filter {

String encoding;

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    encoding = filterConfig.getInitParameter("requestEncoding");
    if (encoding == null) {
        encoding = "UTF-8";
    }
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {

    if(request.getCharacterEncoding()==null) {
        request.setCharacterEncoding(encoding);
    }

    response.setContentType("text/html; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");

    chain.doFilter(request, response);
}

On Tomcat Server.xml setting URIEncoding

<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8" />

Maven in pom.xml

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
<build>
    <outputDirectory>war/WEB-INF/classes</outputDirectory>
        <plugins>
             <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>gwt-maven-plugin</artifactId>
                <version>${gwt.maven}</version>
                <configuration>
                    ...
                    <extraJvmArgs>-Xmx512m **-Dfile.encoding=UTF-8**</extraJvmArgs>
                    ...
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>resources</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            ...

Upvotes: 3

Views: 1371

Answers (2)

WalterP
WalterP

Reputation: 41

After almost 2 days of searching, I have found the solution: Tomcat needs the following JVM argument in order to solve the issue:

-Dfile.encoding=UTF-8 

BR Walter

Upvotes: 1

Mike Brock
Mike Brock

Reputation: 296

Have you considered checking out Errai 2.0? http://errai-blog.blogspot.com/2012/02/quick-tour-of-errai-20.html

Upvotes: 0

Related Questions