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到浏览器
  我们在访问一些大型购物网站的时候,都有添加到购物车这一项,而购物车里面的东西都是临时的,商品买完之后购物车里面的东西可能就没有价值了。如果把这些临时的东西都保存到服务器的话,无疑是一种资源浪费。
776 0
|
2月前
|
网络协议 Java 应用服务中间件
tomcat配置域名及HTTPS
tomcat配置域名及HTTPS
|
2月前
|
Java 应用服务中间件 Windows
【应用服务 App Service】App Service 中部署Java项目,查看Tomcat配置及上传自定义版本
【应用服务 App Service】App Service 中部署Java项目,查看Tomcat配置及上传自定义版本
|
3天前
|
应用服务中间件 Docker 容器
docker应用部署---Tomcat的部署配置
这篇文章介绍了如何使用Docker部署Tomcat服务器,包括搜索和拉取Tomcat镜像、创建容器并设置端口映射和目录映射,以及如何创建一个HTML页面并使用外部机器访问Tomcat服务器。
docker应用部署---Tomcat的部署配置
|
5月前
|
安全 网络协议 应用服务中间件
tomcat 状态页 及常见配置
tomcat 状态页 及常见配置
|
2月前
|
缓存 Java 应用服务中间件
SpringMVC入门到实战------七、SpringMVC创建JSP页面的详细过程+配置模板+实现页面跳转+配置Tomcat。JSP和HTML配置模板的差异对比(二)
这篇文章详细介绍了在SpringMVC中创建JSP页面的全过程,包括项目的创建、配置、Tomcat的设置,以及如何实现页面跳转和配置模板解析器,最后还对比了JSP和HTML模板解析的差异。
SpringMVC入门到实战------七、SpringMVC创建JSP页面的详细过程+配置模板+实现页面跳转+配置Tomcat。JSP和HTML配置模板的差异对比(二)
|
2月前
|
Java 应用服务中间件 开发工具
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
|
2月前
|
Java 应用服务中间件 Windows
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
|
5月前
|
XML Java 应用服务中间件
Tomcat_servlet部署、编译、配置、打包
Tomcat_servlet部署、编译、配置、打包
65 0
|
3月前
|
应用服务中间件
入职必会-开发环境搭建23-IDEA配置Tomcat
IDEA配置Tomcat分为两部分: 1. IDEA集成本地Tomcat 2. IDEA中使用Tomcat部署Web项目 在配置IntelliJ IDEA中的Tomcat时,首先需要打开IDEA,选择菜单中的Run -> Edit Configurations,在左侧菜单中找到+并点击,然后选择Tomcat Server下的Local(注意不要选择错了,下方还有个TomEE Server,不是选这个)。接下来,输入一个自定义的名字作为Tomcat的配置名称,点击Configure...配置Tomcat的安装路径。这样IDEA就配置好了Tomcat。