Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.

A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they handle the optional attributes, so use them sparingly to improve the interoperability of your servlets.

The servlet sends cookies to the browser by using the HttpServletResponse#addCookie method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.

The browser returns cookies to the servlet by adding fields to HTTP request headers. Cookies can be retrieved from a request by using the HttpServletRequest#getCookies method. Several cookies might have the same name but different path attributes.

Cookies affect the caching of the Web pages that use them. HTTP 1.0 does not cache pages that use cookies created with this class. This class does not support the cache control defined with HTTP 1.1.

This class supports both the Version 0 (by Netscape) and Version 1 (by RFC 2109) cookie specifications. By default, cookies are created using Version 0 to ensure the best interoperability.

Author

Various

Constructors

Methods

  • Gets the maximum age in seconds of this Cookie.

    By default, -1 is returned, which indicates that the cookie will persist until browser shutdown.

    Returns number

    an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie persists until browser shutdown

    See

    #setMaxAge

  • Returns the path on the server to which the browser returns this cookie. The cookie is visible to all subpaths on the server.

    Returns string

    a String specifying a path that contains a servlet name, for example, /catalog

    See

    #setPath

  • Returns true if the browser is sending cookies only over a secure protocol, or false if the browser can send cookies using any protocol.

    Returns boolean

    true if the browser uses a secure protocol, false otherwise

    See

    #setSecure

  • Returns the version of the protocol this cookie complies with. Version 1 complies with RFC 2109, and version 0 complies with the original cookie specification drafted by Netscape. Cookies provided by a browser use and identify the browser's cookie version.

    Returns number

    0 if the cookie complies with the original Netscape specification; 1 if the cookie complies with RFC 2109

    See

    #setVersion