Erickson explained this very well on this page. A server-independent solution is to use a character encoding filter, à la org.springframework.web.filter.CharacterEncodingFilter. See example below:
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
private String encoding = "utf-8";
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
filterChain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
String encodingParam = filterConfig.getInitParameter("encoding");
if (encodingParam != null) {
encoding = encodingParam;
}
}
public void destroy() {
// nothing todo
}
}
In web.xml add the filter declaration and the filter url mapping in the appropriate sections:
<filter>
<filter-name>EncodingFilter</filter-name>
<description>
<![CDATA[Changes the encoding of the request, in order to help the appserver to correctly interpret request params]]>
</description>
<filter-class>com.mypackage.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>ISO-8859-15</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>