无法在glassfish 3.1中将字符集从ISO-8859-1更改为UTF-8

时间:2011-06-08 13:30:48

标签: java servlets utf-8 character-encoding glassfish

我在将Web应用程序响应中的字符集从ISO-8859-1(默认)更改为UTF-8时遇到问题。我已经将VM选项-Dfile.encoding=UTF-8添加到JVM选项

但是,我确实从glassfish获得了以下HTTP标头:

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

感谢您的帮助/想法。

4 个答案:

答案 0 :(得分:9)

-Dfile.encoding是一个特定于Oracle JVM的设置,用于说明如何读取Java源文件。这对HTTP响应的Content-Type标头中指定的字符集没有任何影响。

您需要将以下内容添加到web.xml中,以便将所有JSP的响应发送为UTF-8,并让它在响应头中设置相应的字符集。

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

另见:

答案 1 :(得分:4)

对于Glassfish3上的UTF-8字体(日志文件等):

转到Server-config &gt; JVM Settings &gt; JVM Options &gt; {{1 } Add option

如果您不在-Dfile.encoding=UTF8,请转到-server mode &gt; default-config &gt; JVM Settings < / p>

答案 2 :(得分:2)

为了定义除GlassFish(或Tomcat或任何其他Servlet容器)的默认ISO-8859-1之外的标准响应字符集,您需要放置一个调用response.setCharacterEncoding的过滤器。方法如下:
1.在web.xml中定义过滤器:

<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。以下是过滤器实现:

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.");
    }
  }
}

答案 3 :(得分:0)

尝试添加:

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

到你的web.xml ...根据http://wiki.metawerx.net/wiki/Web.xml这些节将你的编码设置为UTF_8。