「Tomcat」Tomcat 6 系と 7 系との Cookie の扱いの違いについて
Tomcat 7 系 ( 厳密には Tomcat 7.0.6 以降 ) では、名前しか持たない Cookie は受け入れないような実装になっているみたいです。
・Apache Tomcat - Migration Guide - Tomcat 7.0.x
http://tomcat.apache.org/migration-7.html#Cookies
Tomcat no longer accepts non-specification compliant name-only cookies by default. However, a new system property has been added, org.apache.tomcat.util.http.ServerCookie.ALLOW_NAME_ONLY, that can be used to accept name-only cookies.
・Apache Tomcat 7 (7.0.47) - Changelog
http://tomcat.apache.org/tomcat-7.0-doc/changelog.html#Tomcat_7.0.6_(markt)
No longer accept specification invalid name only cookies by default. This behaviour can be restored using a system property. (markt)
※ 仕様ってたぶん RFC ( 名前と値のペア ) のことだと思われる。
・RFC 6265 - HTTP State Management Mechanism
http://tools.ietf.org/search/rfc6265#section-4.2
cookie-string = cookie-pair *( ";" SP cookie-pair ) Each cookie-pair represents a cookie stored by the user agent. The cookie-pair contains the cookie-name and cookie-value the user agent received in the Set-Cookie header.
上記の動作については、以下のようなサーブレットと HTML ファイルを作れば確認できる。
package sample; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GetCookieServlet extends HttpServlet { private static final long serialVersionUID = 1L; public GetCookieServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("<html><body>"); Cookie[] cookies = request.getCookies(); if (cookies == null) { out.println("No Cookie !!"); } else { for (Cookie cookie : cookies) { out.println(cookie.getName() + ":" + cookie.getValue()); out.println("<br>"); } } out.println("</body></html>"); } }
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> <!-- function test() { //document.cookie = "name=value"; document.cookie="name"; document.f1.submit(); } // --> </script> </head> <body> <form action="./GetCookieServlet" name="f1"> <input type="button" value="test" onclick="test();" /> </form> </body> </html>
上記を Tomcat 6 系で実行すると画面に「name:」と出力されるが、Tomcat 7 系で実行すると「No Cookie !!」となる。
Migration Guide に書いているようにシステムプロパティ org.apache.tomcat.util.http.ServerCookie.ALLOW_NAME_ONLY を true に設定すると Tomcat 7 系でも「name:」といった出力が得られる ( CATALINA_OPTS="-Dorg.apache.tomcat.util.http.ServerCookie.ALLOW_NAME_ONLY=true" を設定してやればいいですね )。
・Apache Tomcat 7 Configuration Reference (7.0.47) - System Properties
http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html#Specification
[ 検証環境 ]
Apache Tomcat 6.0.35
Apache Tomcat 7.0.27
以上です。