7

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Wintermute
  • 1,521
  • 1
  • 13
  • 34

4 Answers4

9

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:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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 http://stackoverflow.com/questions/4245386/where-to-add-the-utf-8-extension-in-the-html-page/4245514#4245514 If it is indeed missing, are you sure that you didn't set it while the response is already been committed for long? – BalusC Jun 08 '11 at 14:28
  • 1
    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. – Wintermute Jun 09 '11 at 08:50
4

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

Marko
  • 20,385
  • 13
  • 48
  • 64
dimitrios
  • 41
  • 1
2

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.");
    }
  }
}
Omri Spector
  • 2,431
  • 24
  • 22
  • 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. – Miklos Krivan Nov 17 '14 at 05:18
0

Try adding:

    <filter> 
            <filter-name>Set Character Encoding</filter-name> 
            <filter-class>filters.SetCharacterEncodingFilter</filter-class> 
            <init-param> 
                    <param-name>encoding</param-name> 
                    <param-value>UTF_8</param-value> 
            </init-param> 
    </filter> 
    <filter-mapping> 
            <filter-name>Set Character Encoding</filter-name> 
            <url-pattern>/*</url-pattern> 
    </filter-mapping>

to your web.xml... According to http://wiki.metawerx.net/wiki/Web.xml these stanza will set your encoding to UTF_8.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • 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. – Omri Spector Dec 03 '13 at 09:05