How to get tomcat to understand MacRoman (x-mac-ro

2019-09-21 06:53发布

This question already has an answer here:

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条回答
手持菜刀,她持情操
2楼-- · 2019-09-21 06:57

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

查看更多
冷血范
3楼-- · 2019-09-21 07:00

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>
查看更多
We Are One
4楼-- · 2019-09-21 07:08

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:

查看更多
登录 后发表回答