Encoding servlets with UTF-8 on WildFly
Asked Answered
B

3

9

I used to run my JavaEE applications on GlassFish server, and there was no problem with the encoding type (UTF-8) since I added the following property in JVM Settings of the server:

file.encoding = UTF-8

Now, I'm using WildFly server instead, and I've done the supposed configuration to set the encoding type into UTF-8 but characters are still appearing with wrong encoding not only on the web page but also while debugging the application with Eclipse, the response data loaded using (web servlet requests) are not UTF-8 encoded. Below is what I did on WildFly:

snapshot1

snapshot2

Beller answered 3/7, 2015 at 11:4 Comment(1)
Please accept answer from @user158037. Thanks.Bateau
D
23

Wildfly now uses Undertow, so check for that subsystem in you configuration file:

<subsystem xmlns="urn:jboss:domain:undertow:3.0">
      <server name="default-server">
         <http-listener name="default" socket-binding="http" redirect-socket="https" url-charset="UTF-8" />
      </server>
      <servlet-container default-encoding="UTF-8" name="default">
         <jsp-config />
         <websockets />
      </servlet-container>
   </subsystem>

With url-charset and default-encoding set there is no need for filter.

Delimitate answered 28/7, 2016 at 14:15 Comment(0)
P
0

I think, the workaround for these cases, You should implement a Filter before struts filter in web.xml and verify values. After that, you can decoding to unicode into filter and create a interceptor to encoding correctely the values.

That worked to me.

Punjab answered 12/5, 2024 at 1:21 Comment(0)
S
-4

Use a CharacterEncodingFilter (like this one, taken from Spring) to force UTF-8 as the encoding of all requests and responses:

public class CharacterEncodingFilter implements Filter {

    private String encoding;

    private boolean forceEncoding;

    @Override
    public final void init(FilterConfig filterConfig) throws ServletException {
        String encod = filterConfig.getInitParameter("encoding");
        if(encod !=null){
            encoding = encod ;
        }
        String forceEncod = filterConfig.getInitParameter("forceEncoding");
        if(forceEncod !=null){
            forceEncoding = forceEncod ;
        }
    }

    public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
            request.setCharacterEncoding(this.encoding);
            if (this.forceEncoding) {
                response.setCharacterEncoding(this.encoding);
            }
        }
        filterChain.doFilter(request, response);
    }

}

web.xml

<!-- Enforce UTF-8 Character Encoding -->
<filter>
    <filter-name>charEncodingFilter</filter-name>
    <filter-class>somepackages.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
Shrewsbury answered 3/7, 2015 at 11:47 Comment(1)
Even Using a character encoding filter did not solve the problem!Beller

© 2022 - 2025 — McMap. All rights reserved.