这的确是一个不正常的需求,按照规范,开发者需要将cookie进行编码,因为tomcat不支持中文cookie。

但有时候,你不得不面对这样的情况,比如请求是由他人开发的软件,比如,浏览器控件发出的。

这个时候就需要修改tomcat源码来支持了。

直接上源码

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.http; /**
* Static constants for this package.
*/
public final class CookieSupport { // --------------------------------------------------------------- Constants
/**
* If set to true, we parse cookies strictly according to the servlet,
* cookie and HTTP specs by default.
*/
public static final boolean STRICT_SERVLET_COMPLIANCE; /**
* If true, cookie values are allowed to contain an equals character without
* being quoted.
*/
public static final boolean ALLOW_EQUALS_IN_VALUE; /**
* If true, separators that are not explicitly dis-allowed by the v0 cookie
* spec but are disallowed by the HTTP spec will be allowed in v0 cookie
* names and values. These characters are: \"()/:<=>?@[\\]{} Note that the
* inclusion of / depends on the value of {@link #FWD_SLASH_IS_SEPARATOR}.
*/
public static final boolean ALLOW_HTTP_SEPARATORS_IN_V0; /**
* If set to false, we don't use the IE6/7 Max-Age/Expires work around.
* Default is usually true. If STRICT_SERVLET_COMPLIANCE==true then default
* is false. Explicitly setting always takes priority.
*/
public static final boolean ALWAYS_ADD_EXPIRES; /**
* If set to true, the <code>/</code> character will be treated as a
* separator. Default is usually false. If STRICT_SERVLET_COMPLIANCE==true
* then default is true. Explicitly setting always takes priority.
*/
public static final boolean FWD_SLASH_IS_SEPARATOR; /**
* If true, name only cookies will be permitted.
*/
public static final boolean ALLOW_NAME_ONLY; /**
* If set to true, the cookie header will be preserved. In most cases
* except debugging, this is not useful.
*/
public static final boolean PRESERVE_COOKIE_HEADER; /**
* The list of separators that apply to version 0 cookies. To quote the
* spec, these are comma, semi-colon and white-space. The HTTP spec
* definition of linear white space is [CRLF] 1*( SP | HT )
*/
private static final char[] V0_SEPARATORS = {',', ';', ' ', '\t'};
private static final boolean[] V0_SEPARATOR_FLAGS = new boolean[65536]; /**
* The list of separators that apply to version 1 cookies. This may or may
* not include '/' depending on the setting of
* {@link #FWD_SLASH_IS_SEPARATOR}.
*/
private static final char[] HTTP_SEPARATORS;
private static final boolean[] HTTP_SEPARATOR_FLAGS = new boolean[65536]; static {
STRICT_SERVLET_COMPLIANCE = Boolean.parseBoolean(System.getProperty(
"org.apache.catalina.STRICT_SERVLET_COMPLIANCE",
"false")); ALLOW_EQUALS_IN_VALUE = Boolean.parseBoolean(System.getProperty(
"org.apache.tomcat.util.http.ServerCookie.ALLOW_EQUALS_IN_VALUE",
"false")); ALLOW_HTTP_SEPARATORS_IN_V0 = Boolean.parseBoolean(System.getProperty(
"org.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V0",
"false")); String alwaysAddExpires = System.getProperty(
"org.apache.tomcat.util.http.ServerCookie.ALWAYS_ADD_EXPIRES");
if (alwaysAddExpires == null) {
ALWAYS_ADD_EXPIRES = !STRICT_SERVLET_COMPLIANCE;
} else {
ALWAYS_ADD_EXPIRES = Boolean.parseBoolean(alwaysAddExpires);
} String preserveCookieHeader = System.getProperty(
"org.apache.tomcat.util.http.ServerCookie.PRESERVE_COOKIE_HEADER");
if (preserveCookieHeader == null) {
PRESERVE_COOKIE_HEADER = STRICT_SERVLET_COMPLIANCE;
} else {
PRESERVE_COOKIE_HEADER = Boolean.parseBoolean(preserveCookieHeader);
} String fwdSlashIsSeparator = System.getProperty(
"org.apache.tomcat.util.http.ServerCookie.FWD_SLASH_IS_SEPARATOR");
if (fwdSlashIsSeparator == null) {
FWD_SLASH_IS_SEPARATOR = STRICT_SERVLET_COMPLIANCE;
} else {
FWD_SLASH_IS_SEPARATOR = Boolean.parseBoolean(fwdSlashIsSeparator);
} ALLOW_NAME_ONLY = Boolean.parseBoolean(System.getProperty(
"org.apache.tomcat.util.http.ServerCookie.ALLOW_NAME_ONLY",
"false")); /*
Excluding the '/' char by default violates the RFC, but
it looks like a lot of people put '/'
in unquoted values: '/': ; //47
'\t':9 ' ':32 '\"':34 '(':40 ')':41 ',':44 ':':58 ';':59 '<':60
'=':61 '>':62 '?':63 '@':64 '[':91 '\\':92 ']':93 '{':123 '}':125
*/
if (CookieSupport.FWD_SLASH_IS_SEPARATOR) {
HTTP_SEPARATORS = new char[] { '\t', ' ', '\"', '(', ')', ',', '/',
':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '{', '}' };
} else {
HTTP_SEPARATORS = new char[] { '\t', ' ', '\"', '(', ')', ',',
':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '{', '}' };
}
for (int i = 0; i < 65536; i++) {
V0_SEPARATOR_FLAGS[i] = false;
HTTP_SEPARATOR_FLAGS[i] = false;
}
for (int i = 0; i < V0_SEPARATORS.length; i++) {
V0_SEPARATOR_FLAGS[V0_SEPARATORS[i]] = true;
}
for (int i = 0; i < HTTP_SEPARATORS.length; i++) {
HTTP_SEPARATOR_FLAGS[HTTP_SEPARATORS[i]] = true;
} } // ----------------------------------------------------------------- Methods /**
* Returns true if the byte is a separator as defined by V0 of the cookie
* spec.
*/
public static final boolean isV0Separator(final char c) {
// if (c < 0x20 || c >= 0x7f) {
// if (c != 0x09) {
// throw new IllegalArgumentException(
// "Control character in cookie value or attribute.");
// }
// } return V0_SEPARATOR_FLAGS[c];
} public static boolean isV0Token(String value) {
if( value==null) {
return false;
} int i = 0;
int len = value.length(); if (alreadyQuoted(value)) {
i++;
len--;
} for (; i < len; i++) {
char c = value.charAt(i);
if (isV0Separator(c)) {
return true;
}
}
return false;
} /**
* Returns true if the byte is a separator as defined by V1 of the cookie
* spec, RFC2109.
* @throws IllegalArgumentException if a control character was supplied as
* input
*/
public static final boolean isHttpSeparator(final char c) {
// if (c < 0x20 || c >= 0x7f) {
// if (c != 0x09) {
// throw new IllegalArgumentException(
// "Control character in cookie value or attribute.");
// }
// } return HTTP_SEPARATOR_FLAGS[c];
} public static boolean isHttpToken(String value) {
if( value==null) {
return false;
} int i = 0;
int len = value.length(); if (alreadyQuoted(value)) {
i++;
len--;
} for (; i < len; i++) {
char c = value.charAt(i); if (isHttpSeparator(c)) {
return true;
}
}
return false;
} public static boolean alreadyQuoted (String value) {
if (value==null || value.length() < 2) {
return false;
}
return (value.charAt(0)=='\"' && value.charAt(value.length()-1)=='\"');
} // ------------------------------------------------------------- Constructor
private CookieSupport() {
// Utility class. Don't allow instances to be created.
}
}

  

让tomcat支持中文cookie的更多相关文章

  1. 让Tomcat支持中文文件名

    --参考链接:http://blog.chinaunix.net/uid-26284395-id-3044132.html 解决问题的核心在于修改Tomcat的配置,在Server.xml文件中添加一 ...

  2. 让Tomcat支持中文路径名和中文文件名

    http://hdwangyi.iteye.com/blog/107709 Tomcat是Java开发者使用得较多的一个Web服务器,因为它占用资源小,运行速度快等特点,深受Java Web程序员的喜 ...

  3. tomcat支持中文文件名下载

    http://blog.csdn.net/wnczwl369/article/details/7483806 Tomcat 是Java开发者使用得较多的一个Web服务器,因为它占用资源小,运行速度快等 ...

  4. [转载]tomcat的配置文件server.xml不支持中文注释的解决办法

    原文链接:http://tjmljw.iteye.com/blog/1500370 启动tomcat失败,控制台一闪而过,打开catalina的log发现错误指向了conf/server.xml,报错 ...

  5. 1.部分(苹果)移动端的cookie不支持中文字符,2.从json字符串变为json对象时,只支持对象数组

    1.移动端的cookie不支持中文字符.可以用编码,解码的方式解决. 2.json字符串变成相应 的,json对象数组字符串.就这样 3.不同客户端(移动端.电脑)的请求,在C#服务端的取时间的格式竟 ...

  6. 解决tomcat不支持中文路径的问题

    问题描述: 开发文件下载功能时,因为需求比较简单,要求下载一个说明文件.于是,直接给出了文件所在服务器的地址,通过链接直接下载此文件(因需求简单,未考虑安全方面的问题-_-||). 在这个过程中,文件 ...

  7. cookie不支持中文,必须转码后存储,否则会乱码

    cookie不支持中文,必须转码后存储,否则会乱码 Cookie ck = new Cookie("username", URLEncoder.encode(name, " ...

  8. 特大喜讯,View and Data API 现在支持中文界面了

    大家经常会问到,使用View and Data API怎么做界面的本地化,来显示中文,现在好消息来了,从v1.2.19起,View and Data API开始支持多国语言界面了.你需要制定版本号为v ...

  9. tomcat处理中文文件名的访问(乱码)

    解决问题的核心在于修改Tomcat的配置,在Server.xml文件中添加一个名为URIEncoding的属性,它用于对HTTP请求中的get方法传过来的URL进行编码.Tomcat内置的对于get协 ...

随机推荐

  1. Java MyBatis 插入数据库返回主键

    最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...

  2. 微软Azure 经典模式下创建内部负载均衡(ILB)

    微软Azure 经典模式下创建内部负载均衡(ILB) 使用之前一定要注意自己的Azure的模式,老版的为cloud service模式,新版为ARM模式(资源组模式) 本文适用于cloud servi ...

  3. 读python源码--对象模型

    学python的人都知道,python中一切皆是对象,如class生成的对象是对象,class本身也是对象,int是对象,str是对象,dict是对象....所以,我很好奇,python是怎样实现这些 ...

  4. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part2:clusterware安装和升级

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part2:clusterware安装和升级 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 3.安装Clus ...

  5. spring boot 实战:我们的第一款开源软件

    在信息爆炸时代,如何避免持续性信息过剩,使自己变得专注而不是被纷繁的信息所累?每天会看到各种各样的新闻,各种新潮的技术层出不穷,如何筛选出自己所关心的? 各位看官会想,我们是来看开源软件的,你给我扯什 ...

  6. ABP项目中使用Swagger生成动态WebAPI

    本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...

  7. Flexible 弹性盒子模型之CSS flex-basis 属性

    实例 设置第二个弹性盒元素的初始长度为 80 像素: div:nth-of-type(2){flex-basis:80px;}   效果预览 浏览器支持 表格中的数字表示支持该属性的第一个浏览器的版本 ...

  8. BPM始终服务于人,落脚于人

    数字经济时代下,云计算.大数据.移动互联已经成为当下企业必须采取的武装力量.随着互联网+.中国制造2025.工业4.0等国家战略的引导与支持,无数的企业在这场数字化浪潮中使尽浑身解数,想要抓住机遇奋力 ...

  9. iOS 自定义方法 - 不完整边框

    示例代码 ///////////////////////////OC.h////////////////////////// ////  UIView+FreeBorder.h//  BHBFreeB ...

  10. 服务治理要先于SOA

      讲在前面的话: 若企业缺乏对服务变更的控制和规则,那么一个服务在经过几个项目之后,就很有可能被随意更改成多个版本,将来变成什么样更是无法预测.久而久之,降低了服务重用的可能性,提高了服务利用的成本 ...