简体   繁体   English

UTF-8编码不适用于Errai和Tomcat

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

we are developing an web application using GWT 2.4, Errai 1.3.2. 我们正在使用GWT 2.4,Errai 1.3.2开发Web应用程序。 It is running on Tomcat 6 (6.0.35) and built by Maven (3.0.4). 它运行在Tomcat 6(6.0.35)上,由Maven(3.0.4)构建。

When running this application on Tomcat, the transfer of special cases is not working. 在Tomcat上运行此应用程序时,特殊情况的传输无效。 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. 使用errai maven原型时,它具有相同的行为。 When using GWT-RPC instead of errai RPC, everything works fine. 当使用GWT-RPC而不是errai RPC时,一切正常。 Running the same application in Dev-Mode, the problem doesn't occur. 在Dev-Mode中运行相同的应用程序,不会发生此问题。

When looking at the request/response in chrome, both have character encoding UTF-8. 在查看chrome中的请求/响应时,两者都具有UTF-8字符编码。

I think this might be an errai bug because there is some String encoding in errai before sending the response. 我认为这可能是一个错误的错误,因为在发送响应之前,errai中有一些String编码。

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: PS:我已经尝试了以下潜在的解决方案,这些解决方案都不起作用:

Setting index.html head: 设置index.html头:

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

Define a custom Servlet Filter 定义自定义Servlet过滤器

WEB.xml 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 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 在Tomcat Server.xml上设置URIEncoding

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

Maven in pom.xml maven在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>
            ...

After almost 2 days of searching, I have found the solution: Tomcat needs the following JVM argument in order to solve the issue: 经过近两天的搜索,我找到了解决方案:Tomcat需要以下JVM参数才能解决问题:

-Dfile.encoding=UTF-8 

BR Walter BR沃尔特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM