default character encoding of the request or response body:

  If a character encoding is not specified, the Servlet specification requires that an encoding of ISO-8859-1 is used.

  For JSP pages,The request character encoding handling is the same,for JSP pages in standard syntax the default response charset is the usual ISO-8859-1, but for the ones in XML syntax it is UTF-8.

Default encoding for GET

  Many browsers are starting to offer (default) options of encoding URIs using UTF-8 instead of ISO-8859-1.

  HTML 4.0 recommends the use of UTF-8 to encode the query string.

Default Encoding for POST

  ISO-8859-1 is defined as the default character set for HTTP request and response bodies in the servlet specification

change how GET parameters are interpreted

  Tomcat will use ISO-8859-1 as the default character encoding of the entire URL, including the query string ("GET parameters").

  There are two ways to specify how GET parameters are interpreted:

    Set the URIEncoding attribute on the <Connector> element in server.xml to something specific (e.g. URIEncoding="UTF-8").

    Set the useBodyEncodingForURI attribute on the <Connector> element in server.xml to true. This will cause the Connector to use the request body's encoding for GET parameters.

change how POST parameters are interpreted

  POST requests should specify the encoding of the parameters and values they send. Since many clients fail to set an explicit encoding, the default is used (ISO-8859-1).

  In many cases this is not the preferred interpretation so one can employ a javax.servlet.Filter to set request encodings. Writing such a filter is trivial. Furthermore Tomcat already comes with such an example filter. Please take a look at:

5.x

 webapps/servlets-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java
webapps/jsp-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java

6.x

 webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java

5.5.36+, 6.0.36+, 7.x

Since 7.0.20 the filter became first-class citizen and was moved from the examples into core Tomcat and is available to any web application without the need to compile and bundle it separately. See documentation for the list of filters provided by Tomcat. The class name is:

 org.apache.catalina.filters.SetCharacterEncodingFilter

Note:

  The request encoding setting is effective only if it is done earlier than parameters are parsed. Once parsing happens, there is no way back.

  Parameters parsing is triggered by the first method that asks for parameter name or value.

  Make sure that the filter is positioned before any other filters that ask for request parameters.

  The positioning depends on the order of filter-mapping declarations in the WEB-INF/web.xml file, though since Servlet 3.0 specification there are additional options to control the order.

  To check the actual order you can throw an Exception from your page and check its stack trace for filter names.

What can you recommend to just make everything work? (How to use UTF-8 everywhere)

  Using UTF-8 as your character encoding for everything is a safe bet. This should work for pretty much every situation.

  In order to completely switch to using UTF-8, you need to make the following changes:

    1. Set URIEncoding="UTF-8" on your <Connector> in server.xml. References: HTTP ConnectorAJP Connector.

    2. Use a character encoding filter with the default encoding set to UTF-8

    3. Change all your JSPs to include charset name in their contentType.

      For example, use <%@page contentType="text/html; charset=UTF-8" %> for the usual JSP pages and <jsp:directive.page contentType="text/html; charset=UTF-8" /> for the pages in XML syntax (aka JSP Documents).

    4. Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8.

      Use response.setContentType("text/html; charset=UTF-8") or response.setCharacterEncoding("UTF-8").

    5. Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 and to specify UTF-8 in the content type of the responses that they generate.
    6. Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set the encoding to UTF-8. For more information see http://www.mail-archive.com/users@tomcat.apache.org/msg21117.html.

How can I test if my configuration will work correctly?

  The following sample JSP should work on a clean Tomcat install for any input. If you set the URIEncoding="UTF-8" on the connector, it will also work with method="GET".

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Character encoding test page</title>
</head>
<body>
<p>Data posted to this form was:
<%
request.setCharacterEncoding("UTF-8");
out.print(request.getParameter("mydata"));
%> </p>
<form method="POST" action="index.jsp">
<input type="text" name="mydata">
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
</body>
</html>

How can I send higher characters in my HTTP headers?

  You have to encode them in some way before you insert them into a header. Using url-encoding (% + high byte number + low byte number) would be a good idea.

ref:  http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

Character Encoding tomcat.的更多相关文章

  1. 使用tomcat运行时提示some characters cannot be mapped using iso-8859-1 character encoding异常

    今天第一次使用java进行jsp项目搭建,也是第一次使用tomcat.tomcat是运行java web的一个小型服务器,属于Apache的一个开源免费的服务. 在运行web 的时候,我们就要先配置好 ...

  2. java.sql.SQLException: Unsupported character encoding 'utf8mb4'.

    四月 12, 2017 3:47:52 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() fo ...

  3. 使用英文版eclipse保存代码,出现some characters cannot be mapped using "Cp1251" character encoding.

    some characters cannot be mapped using "Cp1251" character encoding. 解决办法:方案一: eclipse-> ...

  4. A - Character Encoding HDU - 6397 - 方程整数解-容斥原理

    A - Character Encoding HDU - 6397 思路 : 隔板法就是在n个元素间的(n-1)个空中插入k-1个板,可以把n个元素分成k组的方法 普通隔板法 求方程 x+y+z=10 ...

  5. Character Encoding in .NET

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-encoding#Encodings Characters ...

  6. hdu6397 Character Encoding 隔板法+容斥原理+线性逆元方程

    题目传送门 题意:给出n,m,k,用m个0到n-1的数字凑出k,问方案数,mod一个值. 题目思路: 首先如果去掉数字范围的限制,那么就是隔板法,先复习一下隔板法. ①k个相同的小球放入m个不同的盒子 ...

  7. some characters cannot be mapped using iso-8859-1 character encoding

    Eclipse中新建一个.properties文件,如果输入中文保存时就会提示错误 Reason:some characters cannot be mapped using "ISO-88 ...

  8. hdu 6397 Character Encoding (生成函数)

    Problem Description In computer science, a character is a letter, a digit, a punctuation mark or som ...

  9. zend studio打开文件提示unsupported character encoding

    zend studio打开文件提示unsupported character encoding,是文件的编码方式错误. 有可能是PHP代码中,charset={CHARSET} ,用了变量的形式调用编 ...

随机推荐

  1. 【深入浅出jQuery】源码浅析2--使用技巧

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  2. [转]Aggregate tasks i Sharepoint 2013

    from http://sharepoint247.com/mysite/aggregate-tasks-i-sharepoint-2013/ Aggregate tasks i Sharepoint ...

  3. 关于Unity3D中的版本管理 .

    关于Unity3D中的版本管理 使用Unity3D也有一段时间了,由于团队一直使用SVN进行版本管理,现总结一下: (1) Unity3D的二进制资源必须加锁进行版本控制,因为它没办法merge: ( ...

  4. XSHELL和XFTP,亲兄弟啊。

    XSHELL在LINUX和WINDOWS之间传输文件时不力啊.又对FTP不灵活的时候,XFTP就可以出场了. 只要登陆进XSHELL就可以操作了.并且XFTP客户端和命令行可以灵活配置选择. 然后,玩 ...

  5. AlgorithmsI Exercises: UnionFind

    Question1 Give the id[] array that results from the following sequence of 6 unionoperations on a set ...

  6. Squares(哈希)

    Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 14328   Accepted: 5393 Description A sq ...

  7. ftp上传下载脚本

    #!/usr/bin/env python #encoding=utf-8 # @Date: 2015-08-10 import datetime from ftplib import FTP &qu ...

  8. 【转】java提高篇(二三)-----HashMap

    原文网址: http://www.cnblogs.com/chenssy/p/3521565.html HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以 ...

  9. MFC重绘函数:InvalidateRect(), Invalidate()和UpdateWindow()

    1. 重绘消息 当需要更新或者重绘窗口时,一般系统会发出两个消息WM_PAINT(通知客户区有变化)和WM_NCPAINT(通知非客户区有变化) WM_NCPAINT系统会自己搞定 WM_PAINT消 ...

  10. android下隐藏标题栏

    最近在开发android,遇到一个问题标题栏影响美观,查了很多网上方法. 用到了方法一: requestWindowFeature(Window.FEATURE_NO_TITLE); 虽然一直强调要在 ...