Tomcat 对 Cookie的聪明处理。

简介:

    近日使用Tomcat调试的时候,使用response写入一个Cookie,发现Cookie的值带上了双引号,百思不得其解,查找源码发现Tomcat在写入Cookie值有"/" 的时候,为避免错误,Tomcat做了以下处理

org.apache.tomcat.util.http.ServerCookie

 

Java代码   收藏代码
  1. <span>    private static void maybeQuote (StringBuffer buf, String value) {  
  2.         if (value==null || value.length()==0) {  
  3.             buf.append("\"\"");  
  4.         } else if (CookieSupport.alreadyQuoted(value)) {  
  5.             buf.append('"');  
  6.             buf.append(escapeDoubleQuotes(value,1,value.length()-1));  
  7.             buf.append('"');  
  8.         } <span style="color: #ff0000;">else if (CookieSupport.isHttpToken(value) &&  
  9.                 !CookieSupport.ALLOW_HTTP_SEPARATORS_IN_V0 ||  
  10.                 CookieSupport.isV0Token(value) &&  
  11.                 CookieSupport.ALLOW_HTTP_SEPARATORS_IN_V0)</span> {  
  12.             buf.append('"');  
  13.             buf.append(escapeDoubleQuotes(value,0,value.length()));  
  14.             buf.append('"');  
  15.         } else {  
  16.             buf.append(value);  
  17.         }  
  18.     }  
  19. </span>  

 查询Tomcat文档,解释如下:

org.apache.catalina. STRICT_SERVLET_COMPLIANCE

If this is true the following actions will occur:

  • any wrapped request or response object passed to an application dispatcher will be checked to ensure that it has wrapped the original request or response. (SRV.8.2 / SRV.14.2.5.1)
  • a call to Response.getWriter() if no character encoding has been specified will result in subsequent calls to Response.getCharacterEncoding() returningISO-8859-1 and the Content-Type response header will include a charset=ISO-8859-1 component. (SRV.15.2.22.1)
  • every request that is associated with a session will cause the session's last accessed time to be updated regardless of whether or not the request explicitly accesses the session. (SRV.7.6)
  • cookies will be parsed strictly, by default v0 cookies will not work with any invalid characters. 
    If set to false, any v0 cookie with invalid character will be switched to a v1 cookie and the value will be quoted.
  • the path in ServletContext.getResource / getResourceAsStream calls must start with a "/".
    If set to false, code like getResource("myfolder/myresource.txt") will work.

 

If this is true the default value will be changed for:

  • org.apache.catalina.connector.Request. ALLOW_EMPTY_QUERY_STRING property
  • The webXmlValidation attribute of any Context element.
  • The webXmlNamespaceAware attribute of any Context element.
  • The tldValidation attribute of any Context element.

 

If not specified, the default value of false will be used.

 

解决办法:

在catalina.properties里边增加一行:

org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true

或者自行修改源码

 影响版本:暂时确认有Tomcat 6、7

 


目录
相关文章
|
Web App开发
Tomcat+Servlet保存Cookie到浏览器
  我们在访问一些大型购物网站的时候,都有添加到购物车这一项,而购物车里面的东西都是临时的,商品买完之后购物车里面的东西可能就没有价值了。如果把这些临时的东西都保存到服务器的话,无疑是一种资源浪费。
760 0
|
2月前
|
XML 应用服务中间件 Apache
Tomcat AJP连接器配置secretRequired=“true“,但是属性secret确实空或者空字符串,这样的组合是无效的。
Tomcat AJP连接器配置secretRequired=“true“,但是属性secret确实空或者空字符串,这样的组合是无效的。
|
28天前
|
前端开发 Java 应用服务中间件
Springboot对MVC、tomcat扩展配置
Springboot对MVC、tomcat扩展配置
|
12天前
|
XML Java 应用服务中间件
Tomcat_servlet部署、编译、配置、打包
Tomcat_servlet部署、编译、配置、打包
16 0
|
25天前
|
运维 Java 应用服务中间件
Tomcat详解(二)——tomcat安装与配置
Tomcat详解(二)——tomcat安装与配置
21 1
|
12天前
|
网络协议 Java 应用服务中间件
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
9 0
|
13天前
|
应用服务中间件
【SSM】如何在IDEA配置tomcat启动项目
【SSM】如何在IDEA配置tomcat启动项目
18 1
|
13天前
|
IDE JavaScript Java
如何配置tomcat
【4月更文挑战第15天】如何配置tomcat
17 2
|
14天前
|
负载均衡 Ubuntu 应用服务中间件
Apache(mod_proxy)+Tomcat负载均衡配置
Apache(mod_proxy)+Tomcat负载均衡配置
|
14天前
|
Java 应用服务中间件 Apache
Tomcat7 的安装和配置
Tomcat7 的安装和配置
16 0