Setting Cookie Attributes in Java

Invoke one of the following methods to set attributes of a cookie.
 
 
Method Meaning
setComment(String) Sets a comment associated with the Cookie.  Informational only
getComment() Returns String representing Comment previously associated with this Cookie
setDomain(String) Pass as a String the Domain name you want the cookie associated with.
Default:  will be the domain from which the cookie was originally sent.  But, if you want the cookie to be sent to a different domain, use this method
getDomain() Retrieves the domain associated with this cookie
setMaxAge(int seconds) Sets in seconds the time that should elapse before the cookie expires.
  • If negative, cookie will last only for current session (until user exits browser) and will NOT be stored on disk.  default.
  • Value of 0 meeans the browser should delete the cookie.
getMaxAge() Returns int, number of seconds of lifetime of Cookie
setName(String) This is done usually at Construction time of the Cookie, but, if you wish to change the name of the binding stored in the cookie you can do so
getName() Returns String, represents name of binding stored in Cookie
setPath(String) Specifies the root path, under which all paths, at the given domain are sent the cookie. 
 
  • Default: The root path is the path that originally lent to the Cookie being sent to the client.
    • If Cookie sent from http://f.com/books/process.html

    • Then, the following URLs requested by the client would result in the client's browser automatically sending the cookie:
       
        http://f.com/books/fiction/find.htm
        http://f.com/books/search.html
      The following would NOT recieve the Cookie:
       
        http://f.com/cds/search.html
        http://f.com

       
  • If you set the Path to "/", then any path on the given domain will be sent this cookie.
getPath() Retrieve as String the root path to which the cookie applies
setSecure(boolean) If boolean true, sends the cookie only over encrypted connections. 
If boolean false, will send over any connectgion
getSecure() Returns boolean indicating whether encryption connection required to send cookie.
setValue(String) This is done usually at Construction time of the Cookie, but, if you wish to change the value of the binding stored in the cookie you can do so
getValue() Returns String associated with the value of the binding stored in the Cookie

© Lynne Grewe 2000