java servlets utf-8 character-encoding glassfish

java - No se puede cambiar el juego de caracteres de ISO-8859-1 a UTF-8 en glassfish 3.1



servlets character-encoding (4)

Tengo problemas para cambiar el juego de caracteres en la respuesta de mi aplicación web desde ISO-8859-1 (predeterminado) a UTF-8. Ya agregué la opción VM -Dfile.encoding=UTF-8 a las opciones de JVM

Pero aún así, obtengo el siguiente encabezado HTTP como respuesta del glassfish:

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

Apreciaría su ayuda / ideas.


El -Dfile.encoding es una configuración específica de Oracle JVM sobre cómo leer los archivos fuente de Java. Esto no tiene ninguna influencia en el juego de caracteres como se especifica en el encabezado Content-Type de la respuesta HTTP.

Debe agregar lo siguiente a su web.xml para enviar la respuesta de todos los JSP como UTF-8 y dejar que establezca el juego de caracteres apropiado en el encabezado de respuesta.

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

Ver también:


Para definir un conjunto de caracteres de respuesta estándar que no sea el predeterminado ISO-8859-1 para GlassFish (o Tomcat, o cualquier otro contenedor de servlets), deberá colocar un filtro que invoque response.setCharacterEncoding. Aquí es cómo:
1. En su web.xml defina el filtro:

<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. Aquí está la implementación del filtro:

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


Para las fuentes UTF-8 en Glassfish3 (archivos de registro, etc.):

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

Si no está en el -server mode vaya a default-config > JVM Settings JVM Options > JVM Options


Prueba agregar:

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

a su web.xml ... Según http://wiki.metawerx.net/wiki/Web.xml, esta estrofa configurará su codificación en UTF_8.