How to set tomcat 8 container character encoding of request and response to UTF-8 intead of ISO-8859-1

我的未来我决定 提交于 2019-11-27 03:40:34

问题


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


回答1:


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

This is described in Tomcat 8 Container Provided Filters.

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

The following configuration works for

  • Tomcat 8.0.30
  • jdk1.8.0_66
  • SQL Server 2008 R2
  • Microsoft JDBC driver (sqljdbc42.jar)

Character Encoding Filter

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 Page Encoding

web.xml

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

JSP Page Creation

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

Database Character Types

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

Database Connection String

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.

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

Strict Servlet compliance

Do not set 'strict servlet compliance'.

From the Tomcat 8 Migration Guide

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)

File System Character Encoding

Iterating & displaying contents of:

System.getProperties();

gives:

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

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




回答2:


Character encoding filter should be the first filter in the filter chain that accesses request parameters.




回答3:


for me it was enough to setup tomcat's web.inf (/tomcat/8.0.33/conf/web.inf), specifically header.

instead of :

<?xml version="1.0" encoding="ISO-8859-1"?>

i'm using now :

<?xml version="1.0" encoding="UTF-8"?>



回答4:


I had the same issue with HTML pages returned by the controllers. there was no problem while running in IntelliJ IDEA 2018 but after building the war file and deploying it in tomcat there was encoding problem with HTML pages which were in project static folder. after a bit searching, I found this life-saving answer and I added the below tag in the web.xml file of tomcat in C:\apache-tomcat\conf directory and everything just worked like a charm.

    <mime-mapping>
        <extension>html</extension>
        <mime-type>text/html;charset=UTF-8</mime-type>
     </mime-mapping>


来源:https://stackoverflow.com/questions/35431915/how-to-set-tomcat-8-container-character-encoding-of-request-and-response-to-utf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!