Unable to change charset from ISO-8859-1 to UTF-8 in glassfish 3.1

The -Dfile.encoding is a Oracle JVM specific setting as to how to read Java source files. This doesn't have any influence on the charset as specified in the Content-Type header of the HTTP response.

You need to add the following to your web.xml in order to send the response of all JSPs as UTF-8 and to let it set the appropriate charset in the response header.

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

See also:

  • Unicode - How to get the characters right?

For UTF-8 fonts on Glassfish3 (Log files, etc):

Go to Server-config > JVM Settings > JVM Options > Add option (-Dfile.encoding=UTF8).

If you are not on -server mode then go to default-config > JVM Settings > JVM Options


In order to define a standard response charset other than the default ISO-8859-1 for GlassFish (or Tomcat, or any other Servlet container) you will need to put a filter that calls response.setCharacterEncoding. Here is how:
1. In your web.xml define the filter:

<filter>
  <filter-name>Set Response Character Encoding</filter-name>
  <filter-class>com.omrispector.util.SetCharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
</filter>

<filter-mapping>
  <filter-name>Set Response Character Encoding</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

2. Here is the filter implementation:

package com.omrispector.util;

import javax.servlet.*;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.logging.Logger;

/**
 * Created by Omri at 03/12/13 10:39
 * Sets the character encoding to be used for all sources returned
 * (Unless they override it later)
 * This is free for use - no license whatsoever.
 */
public class SetCharacterEncodingFilter implements Filter {

  private String encoding = null;
  private boolean active = false;

  private static final Logger logger =
      Logger.getLogger(SetCharacterEncodingFilter.class.getName());

  /**
   * Take this filter out of service.
   */
  @Override
  public void destroy() {
    this.encoding = null;
  }

  /**
   * Select and set (if specified) the character encoding to be used to
   * interpret request parameters for this request.
   */
  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain chain)
      throws IOException, ServletException {
    if (active) response.setCharacterEncoding(encoding);
    chain.doFilter(request, response);
  }

  /**
   * Place this filter into service.
   */
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    this.encoding = filterConfig.getInitParameter("encoding");
    try {
      Charset testCS = Charset.forName(this.encoding);
      this.active = true;
    } catch (Exception e) {
      this.active = false;
      logger.warning(encoding + " character set not supported ("+e.getMessage()+"). SetCharacterEncodingFilter de-activated.");
    }
  }
}