Чтобы определить стандартную кодировку ответа, отличную от ISO-8859-1 по умолчанию для GlassFish (или Tomcat, или любого другого контейнера сервлета), вам необходимо установить фильтр, который вызывает 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.");
}
}
}