`
小码哥BASE64
  • 浏览: 123570 次
社区版块
存档分类
最新评论

Tomcat 对 Cookie的聪明处理。

阅读更多

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

org.apache.tomcat.util.http.ServerCookie

 

    private static void maybeQuote (StringBuffer buf, String value) {
        if (value==null || value.length()==0) {
            buf.append("\"\"");
        } else if (CookieSupport.alreadyQuoted(value)) {
            buf.append('"');
            buf.append(escapeDoubleQuotes(value,1,value.length()-1));
            buf.append('"');
        } else if (CookieSupport.isHttpToken(value) &&
                !CookieSupport.ALLOW_HTTP_SEPARATORS_IN_V0 ||
                CookieSupport.isV0Token(value) &&
                CookieSupport.ALLOW_HTTP_SEPARATORS_IN_V0) {
            buf.append('"');
            buf.append(escapeDoubleQuotes(value,0,value.length()));
            buf.append('"');
        } else {
            buf.append(value);
        }
    }

 查询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

 

 

 

 

分享到:
评论
1 楼 Tomcat911 2014-05-06  
高手

相关推荐

    tomcat修改jsessionid在cookie中的名称

    为了更好地理解上述配置是如何工作的,接下来将对Tomcat 5.x 和 6.x/7.x 的源代码进行简单的分析。 ##### 1. Tomcat 5.x 源码解析 在Tomcat 5.x 中,`SESSION_COOKIE_NAME` 的定义如下: ```java public static ...

    tomcat启动不了问题处理

    ### tomcat启动不了问题处理 在IT领域,Tomcat服务器是一款非常流行的开源Web服务器,它主要用来部署Java Web应用程序。然而,在使用过程中,用户可能会遇到Tomcat无法启动的问题。本文将详细探讨导致Tomcat启动...

    tomcat处理一个http请求的详细过程

    tomcat中server配置文件的结构,以及处理一个http请求的全过程

    tomcat7tomcat8tomcat9

    此外,Tomcat 8在性能和安全性上都有所提升,增强了对大型应用的处理能力。 Tomcat 9则是在2013年晚些时候发布,主要增加了对Java EE 7的支持,包括WebSocket 1.1、JMS 2.0和JSON-P等新特性。它改进了多线程模型,...

    tomcat7,tomcat8,tomcat9

    Tomcat8的另一个亮点是对WebSocket协议的支持,这使得开发实时Web应用变得更加容易。 **Tomcat9**: Tomcat9是在2014年推出,主要支持Java Servlet 4.0和JSP 2.3规范。此版本进一步提升了性能和安全性,尤其是在...

    tomcat 7 和 tomcat 8

    1. 发布时间:Tomcat 7在2011年发布,作为Tomcat 6的升级版,提供了对Java EE 6的支持。 2. Java EE支持:Tomcat 7支持Servlet 3.0、JSP 2.2、EL 2.2和WebSocket 1.0等规范,使得开发者能够利用新的特性进行Web应用...

    tomcat5.5tomcat最新客户端

    tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat最新客户端tomcat...

    tomcat 环境配置及问题处理

    ### Tomcat环境配置详解及问题处理 #### 一、Tomcat简介 Apache Tomcat是一款开源的Servlet容器,它能够提供对Servlet和JavaServer Pages技术的支持,并且可以作为HTTP Web服务器来使用。通常用于开发和部署Web...

    Tomcat7.0 tomcat最新版本

    1. **Servlet 3.0支持**:Tomcat 7.0引入了对Servlet 3.0规范的支持,这意味着开发者可以利用新的特性,如异步处理、注解配置、动态注册Servlet和过滤器,以及初始化参数等,简化开发流程。 2. **JSP 2.2支持**:...

    基于tomcat服务器的跨域处理

    基于tomcat服务器的跨域处理, 包含cors-filter-1.7.jar,Java -property-utils-1.9.jar 配置跨域 <!--跨域-->  <groupId>com.thetransactioncompany</groupId>  <artifactId>Java-property-utils  <version>...

    tomcat 7 最新版本 apache-tomcat-7.0.109

    1. **Servlet 3.0支持**:Tomcat 7全面支持Servlet 3.0规范,这引入了诸如异步处理、注解配置、动态部署等功能,提高了Web应用的开发效率和性能。 2. **JSP 2.2支持**:它包含对JSP 2.2的实现,提供了更多的EL...

    tomcat 5.0 tomcat 5.0

    tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0tomcat 5.0

    Tomcat Tomcat Tomcat Tomcat

    Tomcat,全称Apache Tomcat,是Apache软件基金会的一个开源项目,它是一个实现了Java Servlet和JavaServer Pages(JSP)技术的Web应用服务器,主要用于处理Java的动态内容。Tomcat是轻量级的,相比其他如IBM ...

    tomcat源码

    3. **会话管理**:Tomcat如何实现Session跟踪,包括Cookie、URL重写和基于数据库的持久化策略。 4. **连接器与协议处理**:Coyote如何处理HTTP请求,包括解析请求头、读取请求体、构建响应,以及处理Keep-Alive和...

    Tomcat性能调优方案

    虽然Tomcat也可以作web服务器,但其处理静态html的速度比不上Apache,且其作为web服务器的功能远不如Apache,因此把Apache和Tomcat集成起来,将html和Jsp的功能部分进行明确分工,让Tomcat只处理Jsp部分,其他的由...

    tomcat 系列tomcat 系列

    2. 使用负载均衡器:如Nginx、Apache HTTP Server,实现对多台Tomcat的智能调度。 总结,Apache Tomcat是Java Web开发的重要组成部分,理解其工作原理、配置、优化及故障排查是每个Java开发者必备的技能。通过不断...

    tomcat6的源码

    2. **线程模型**:Tomcat使用了多线程模型来处理并发请求。源码中的`Catalina`和` Coyote`组件涉及到了这部分内容,包括Acceptor线程和Worker线程的管理。 3. **生命周期管理**:每个Tomcat组件都有自己的生命周期...

    webServer tomcat5 / tomcat6 / tomcat7 / tomcat8

    1. **Tomcat 5**:这是Tomcat的一个重大升级,主要引入了对Servlet 2.4和JSP 2.0规范的支持。此外,它增强了安全性,提供了更好的管理工具,以及对NIO(非阻塞I/O)的支持,提高了并发性能。 2. **Tomcat 6**:此...

    idea tomcat runner插件

    4. **配置管理**:支持对Tomcat的配置进行自定义,包括端口号、工作目录等,满足不同项目的特定需求。 5. **日志查看**:方便查看Tomcat服务器的日志输出,帮助开发者定位和解决问题。 **二、安装与使用** 1. **...

    Tomcat7及Tomcat8

    2. **Jasper增强**:Tomcat 7的Jasper引擎对JSP编译进行了优化,支持JSP 2.2标准,提供了更多的错误检查和改进的开发体验。 3. **更好的安全管理**:增加了基于角色的安全管理(RBAC),可以更精细地控制用户的访问...

Global site tag (gtag.js) - Google Analytics