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

22,254

Solution 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:

Solution 2

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

Solution 3

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.");
    }
  }
}
Share:
22,254
Wintermute
Author by

Wintermute

Software architect and developer with a focus on Java and JEE. Also interested in quality assurance technology and methods.

Updated on December 03, 2020

Comments

  • Wintermute
    Wintermute over 3 years

    I am having problems to change the charset in my web application response from ISO-8859-1 (default) to UTF-8. I already added the VM option -Dfile.encoding=UTF-8 to the JVM options

    But still, I do get the following HTTP Header as a response from the glassfish:

    Content-Type: [...;charset=ISO-8859-1]
    Server: [GlassFish Server Open Source Edition 3.1]
    

    I would appreciate your help/ideas.

  • BalusC
    BalusC almost 13 years
    That line ought to be sufficient to get it in the header (note that the meta tag is ignored when the resource is served over HTTP, it's the HTTP response header which counts). Your problem is caused by something else. To recap, do you really get the Content-Type header back with a different encoding? Check it by Firebug and on. See also the screen in this answer stackoverflow.com/questions/4245386/… If it is indeed missing, are you sure that you didn't set it while the response is already been committed for long?
  • Wintermute
    Wintermute almost 13 years
    Damn, if I would have read the specification thoroughly... (From ServletResponse.setCharacterEncoding() : ...This method has no effect if it is called after getWriter has been called...). We are setting the contentType to "text-html" with no charset, then call getWriter and then I try to override the charset with UTF-8. But this is ignored and the default ISO-8859-1 is used. So I need to find out, how to set the charset to default to UTF-8 or change the code.
  • Omri Spector
    Omri Spector over 10 years
    filters.SerCharacterEncodingFilter handles request encoding, not response. Also it is a Tomcat class, not available in GlassFish. See my answer below for a comparable solution that is relevant.
  • Miklos Krivan
    Miklos Krivan over 9 years
    Your solution worked for me in Glassfish 3.1 in Windows 7 environment. I had the same problem delivering static content from Glassfish to browser. Even the meta tag was defined with UTF-8 encoding the server delivered it as ISO-8859-1 before. Thx for your article now it works perfect.