I have a Tomcat 7 webapp and am having problems with character sets. My goal is to force everything into UTF-8 and just be done with it. I'm actually surprised that in 2014 not everything defaults to UTF-8...
I read the docs and have uncommented the org.apache.catalina.filters.AddDefaultCharsetFilter
filter in the system's default web.xml.
/etc/tomcat/web.xml:
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>setCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I have also added URIEncoding="UTF-8"
to the Connectors in the server.xml:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
Doing this (and a bunch of other stuff like jdbc params) seems to get the request into UTF-8. But how do I force the Response to UTF-8?
i.e.
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.printf("Req: %s\n", req.getCharacterEncoding());
System.out.printf("Resp: %s\n", resp.getCharacterEncoding());
yields:
Req: UTF-8
Resp: ISO-8859-1
Thanks