如何将请求和响应的 tomcat 8 容器字符编码设置为 UTF-8 而不是 ISO-8859-1 [英] How to set tomcat 8 container character encoding of request and response to UTF-8 intead of ISO-8859-1

查看:38
本文介绍了如何将请求和响应的 tomcat 8 容器字符编码设置为 UTF-8 而不是 ISO-8859-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要将请求和响应的tomcat 8容器字符编码设置为UTF-8而不是ISO-8859-1,相同的设置是什么我们尝试如下设置,https://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q1

We need to set tomcat 8 container character encoding of request and response to UTF-8 intead of ISO-8859-1 , What is the setting for the same We tried setting as mentioned below , https://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q1

但这需要创建过滤器等.有没有什么优雅的方法可以让我们更改一些配置集以使其适用于容器级别

But that requires creating filter etc. Is there any elegant way where we can just change some configuration set to make it applicable at container level

推荐答案

Tomcat 8+ 捆绑了一个过滤器来设置字符编码.

Tomcat 8+ comes bundled with a filter to set the character encoding.

这在 Tomcat 8 容器提供的过滤器.

需要在您的 web.xml 文件中配置此过滤器,并进行一些其他更改,如下所示.

This filter needs to be configured in your web.xml file plus a few other changes as below.

以下配置适用于

  • Tomcat 8.0.30
  • jdk1.8.0_66
  • SQL Server 2008 R2
  • Microsoft JDBC 驱动程序 (sqljdbc42.jar)

字符编码过滤器

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

<filter-mapping>
  <filter-name>setCharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

JSP 页面编码

web.xml

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <page-encoding>UTF-8</page-encoding>
  </jsp-property-group>
</jsp-config>

JSP 页面创建

只能使用标准英文字符即没有重音字符等

Only standard English characters should be used ie no accented characters etc

数据库字符类型

表列的所有字符类型都应该是 Unicode 类型,例如 NCHAR、NVARCHAR &文本.

All character types for table columns should be Unicode types eg NCHAR, NVARCHAR & NTEXT.

数据库连接字符串

对于排序规则为Latin1_General_CI_AS"的 SQL Server 2008 R2 数据库,似乎不需要将连接字符串设置为使用 UTF-8.

For a SQL Server 2008 R2 database with collation 'Latin1_General_CI_AS', it seems that the connection string does not need to be set to use UTF-8.

上下文.xml

 <Resource name="jdbc/gtraxDS" auth="Container"
           type="javax.sql.DataSource"  
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
            url="jdbc:sqlserver://ctceu-wgc-dv026:45666; databaseName=gtraxd2"
       username="myAppUsername"
       password="myAppPa55word"/>

严格的 Servlet 合规性

不要设置严格的 servlet 遵从性".

Do not set 'strict servlet compliance'.

来自 Tomcat 8 迁移指南

The default value of URIEncoding attribute for HTTP and AJP connectors has been changed from
"ISO-8859-1" to be "UTF-8" (if "strict servlet compliance" mode is off, which is the default)

文件系统字符编码

迭代&显示内容:

Iterating & displaying contents of:

System.getProperties();

给出:

...
file.encoding       Cp1252
file.encoding.pkg   sun.io
...

好像不需要改文件编码了

It seems that the file encoding does not need to be changed

这篇关于如何将请求和响应的 tomcat 8 容器字符编码设置为 UTF-8 而不是 ISO-8859-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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