Class Name: javax.servlet.http.Cookie

Superclass: java.lang.Object

Immediate Subclasses: None

Interfaces Implemented: java.lang.Cloneable

Availability: Servlet API 2.0 and later


The Cookie class provides an easy way for servlets to read, create, and manipulate HTTP-style cookies, which allow servlets to store small amounts of data on the client. Cookies are generally used for session tracking or storing small amounts of user-specific configuration information. For more information, consult Chapter 7.

A servlet uses the getCookies( ) method of HttpServletRequest to retrieve cookies submitted as part of a client request. The addCookie( ) method of HttpServletResponse sends a new cookie to the browser. Because cookies are set using HTTP headers, addCookie( ) must be called before the response is committed.

The get XXX () methods are rarely used because when a cookie is sent to the server, it contains only its name, value, and version. If you set an attribute on a cookie received from the client, you must add it to the response for the change to take effect, and you should take care that all attributes except name, value, and version are reset on the cookie as well.

This class supports both the Netscape cookie specification and RFC 2109.

Class Summary

public class Cookie implements java.lang.Cloneable { // Constructors public Cookie(String name, String value); // Instance methods public Object clone(); public String getComment(); public String getDomain(); public ...

Get Java Servlet Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.