Tomcat 7 org.apache.catalina.filters.AddDefaultCharsetFilter不使用UTF-8 [英] Tomcat 7 org.apache.catalina.filters.AddDefaultCharsetFilter not using UTF-8

查看:965
本文介绍了Tomcat 7 org.apache.catalina.filters.AddDefaultCharsetFilter不使用UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Tomcat 7 webapp,我有字符集的问题。我的目标是强制一切都进入UTF-8,只是做它。我真的很惊讶,在2014年,不是一切都默认为UTF-8 ...

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...

我阅读文档,并取消注释 org.apache .catalina.filters.AddDefaultCharsetFilter 在系统的默认web.xml中过滤。

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:

/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>

我还添加了 URIEncoding =UTF-8到server.xml中的连接器:

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"/>

这样做(和一堆其他东西,如jdbc params)似乎得到请求到UTF- 8。但是如何强制响应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?

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());

产生:

Req: UTF-8
Resp: ISO-8859-1

感谢

推荐答案

看起来您正在讨论两个不同的过滤器:

It looks like you are talking about two different filters:


  • AddDefaultCharsetFilter (适用于回应

  • SetCharacterEncodingFilter (适用于请求

  • AddDefaultCharsetFilter (applies to response)
  • SetCharacterEncodingFilter (applies to request)

在您的情况下, > AddDefaultCharsetFilter ,如下所述:

In your case, you want to use AddDefaultCharsetFilter as explained here:

http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Add_Default_Character_Set_Filter

这篇关于Tomcat 7 org.apache.catalina.filters.AddDefaultCharsetFilter不使用UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆