Class Cookie

Package javax.servlet.http

public class Cookie
extends java.lang.Object
implements java.lang.Cloneable

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(javax.servlet.http.Cookie) method, which adds fields to HTTP response headers to send cookies to the browser, one at a time.

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.

Class Cookie Constructor

Constructor Summary
Cookie(java.lang.String name, java.lang.String value)
Constructs a cookie with a specified name and value.

Class Cookie Constructor Detail


public Cookie(java.lang.String name,
java.lang.String value)

Constructs a cookie with a specified name and value.

The name must conform to RFC 2109. That means it can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character. The cookie's name cannot be changed after creation.

The value can be anything the server chooses to send. Its value is probably of interest only to the server. The cookie's value can be changed after creation with the setValue method.

Cookies are created according to the Netscape cookie specification. The version can be changed with the setVersion method.


name - a String specifying the name of the cookie
value - a String specifying the value of the cookie


java.lang.IllegalArgumentException - if the cookie name contains illegal characters or it is one of the tokens reserved for use by the cookie protocol

Class Cookie Method

Method Summary
Overrides the standard java.lang.Object.clone method to return a copy of this cookie.
Returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
Returns the domain name set for this cookie.
Returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown.
Returns the name of the cookie.
Returns the path on the server to which the browser returns this cookie.
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 the value of the cookie.
Returns the version of the protocol this cookie complies with.
voidsetComment(java.lang.String purpose)
Specifies a comment that describes a cookie's purpose.

References By : apache « Privious Post » « Next Post » share on :        :  

Love to hear your Views / Guidance / Recommendations on this Post…

Explore the Technology World

Real Time Analytics with Spring Application Development FrameworkSpring is the most popular application development framework for enterprise Java.Millions of developers use Spring to build simple, truly portable, fast and fle ...Read MorePath Setting in Java When an application is launched from command prompt Windows OS searches for the executable program in the current working directory. The prompt throws an error ...Read MorePicture-in-Picture App for Android devices with Source CodePicture-in-Picture is a special type of multi-window mode mostly used for video playback for Android devices.Released under Apache License 2.0 , which means you ...Read Moreopen source free template for personal or company blogsLooking for perfect theme for personal or company blogs.Released under the MIT license, which means you can use this template for any purpose, even for commerci ...Read MoreHow to Build an App for the Google AssistantApps for the Google Assistant are the gateway for your users to engage with your services through Google Home, Android phones, iPhones.Watch here to build an as ...Read MoreIntroduction to AMP - Accelerated Mobile PagesAMP, short for Accelerated Mobile Pages, is new new open source initiative to make the mobile web awesome again.Watch Here the Introduction to AMP ...Read MoreFacebook |Google+ | Twitter | LinkedIn | Youtube About | Cookies-Policy | Disclaimer | Site Map | Contact

Call / Visit for New Batch


Why Join Us

  • ➯ Free Demo Classes
  • ➯ No Registration Fee
  • ➯ Interview Questions
  • ➯ Study Materials
  • ➯ Softwares
  • ➯ Aptitude & Reasoning
  • ➯ Placement Assitance


  • MyJava Training Institute ,
  • Plot No.243 , shop No 14 ,
  • Secnd Floor , Zone-II ,
  • M.P.Nagar , Bhopal,
  • Madhya Pradesh ( 462011 )
  • ☎  08871209400 , 7987028543
  • 🌐
FacebookGoogle +TwitterYouTube

MyJava - java Training Institute