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

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

但是,我仍然得到以下HTTP标头作为来自glassfish的响应:

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

非常感谢您的帮助/想法。

-Dfile.encoding是关于如何读取Java源文件的Oracle JVM特定设置。 这对HTTP响应的Content-Type标头中指定的charset没有任何影响。

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

   *.jsp UTF-8   

也可以看看:

  • Unicode – 如何使角色正确?

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

转至Server-config > JVM Settings > JVM Options > Add option -Dfile.encoding=UTF8

如果您未处于服务器-server mode请转至default-config > JVM Settings > JVM Options

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

  Set Response Character Encoding com.omrispector.util.SetCharacterEncodingFilter  encoding UTF-8    Set Response Character Encoding /*  

2.这是filter实现:

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

尝试添加:

   Set Character Encoding filters.SetCharacterEncodingFilter  encoding UTF_8    Set Character Encoding /*  

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