My Mac keyboard sends x-mac-roman charset to Tomcat. Is there a way to force my browser or tomcat to somehow understand what I type on my mac?
3 Answers
You need to ensure that each component is using the same character set at every stage.
To force Tomcat to use a particular character encoding you'll need to set URIEncoding on the connector. In this case you probably want URIEncoding="x-MacRoman"
You may also find this useful: http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

- 16,339
- 1
- 39
- 60
-
1Setting `URIEncoding` to a Mac OS specific character encoding will cause the webpage to fail for clients using a different operating system platform. – BalusC Apr 30 '12 at 19:36
Use UTF-8 instead. It's understood by every self-respected client side and server side operating system platform. It's also considered the standard character encoding these days. Tomcat still defaults to the old ISO 8859-1 charset as to decoding GET request parameters and to the server platform default encoding as to decoding POST request parameters.
To set the GET request encoding, edit the desired HTTP connector in Tomcat's /conf/server.xml
:
<Connector ... URIEncoding="UTF-8">
To set the POST request encoding, create a servlet filter which basically does the following in the doFilter()
method and map it on the URL-pattern matching all POST requests such as *.jsp
or /*
:
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
To tell JSP to use the specified encoding on generating the response (you still need to make sure that you save the JSP files using the very same encoding) which will implicitly also tell the client to use UTF-8 to interpret the response and encode the subsequent request, set the following in top of every JSP:
<%@page pageEncoding="UTF-8"%>
Or set the following configuration entry in webapp's web.xml
, so that it get applied on every single JSP:
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
If you happen to use a database, don't forget to set the proper encoding in there as well.
See also:
The problem was related to POST requests. Tomcat is not good with them. The fix: add this filter to all your requests: http://wiki.apache.org/cocoon/SetCharacterEncodingFilter
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

- 6,007
- 14
- 51
- 85
-
1Tomcat handles POST that conform the the HTTP specification perfectly well. Browsers on the other hand are not good at following the specification. Read the FAQ I already pointed you towards. It covers GET and POST. – Mark Thomas Apr 29 '12 at 20:08