-2

我的 Mac 键盘将 x-mac-roman 字符集发送到 Tomcat。有没有办法强制我的浏览器或 tomcat 以某种方式理解我在我的 mac 上输入的内容?

4

3 回答 3

3

您需要确保每个组件在每个阶段都使用相同的字符集。

要强制 Tomcat 使用特定的字符编码,您需要在连接器上设置 URIEncoding。在这种情况下,您可能需要 URIEncoding="x-MacRoman"

您可能还会发现这很有用:http ://wiki.apache.org/tomcat/FAQ/CharacterEncoding

于 2012-04-29T09:44:53.113 回答
2

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:

于 2012-04-30T19:41:20.523 回答
0

该问题与 POST 请求有关。Tomcat 对他们不好。修复:将此过滤器添加到您的所有请求中: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>
于 2012-04-29T19:59:14.183 回答