Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
184
Chapter 8 Handling Cookies
getVersion
and
setVersion
), the comment is used purely for
informational purposes on the server; it is not sent to the client.
public String getDomain()
public void setDomain(String domainPattern)
These methods get or set the domain to which the cookie applies.
Normally, the browser only returns cookies to the exact same host
name that sent them. You can use
setDomain
method to instruct the
browser to return them to other hosts within the same domain. To
prevent servers setting cookies that apply to hosts outside their
domain, the domain specified is required to start with a dot (e.g.,
.prenhall.com
), and must contain two dots for noncountry domains
like
.com
,
.edu
and
.gov
; and three dots for country domains like
.co.uk
and
.edu.es
. For instance, cookies sent from a servlet at
bali.vacations.com
would not normally get sent by the browser to
pages at
mexico.vacations.com
. If the site wanted this to happen,
the servlets could specify
cookie.setDomain(".vacations.com")
.
public int getMaxAge()
public void setMaxAge(int lifetime)
These methods tell how much time (in seconds) should elapse before
the cookie expires. A negative value, which is the default, indicates that
the cookie will last only for the current session (i.e., until the user quits
the browser) and will not be stored on disk. See the
LongLivedCookie
class (Listing 8.4), which defines a subclass of
Cookie
with a maximum
age automatically set one year in the future. Specifying a value of 0
instructs the browser to delete the cookie.
public String getName()
public void setName(String cookieName)
This pair of methods gets or sets the name of the cookie. The name and
the value are the two pieces you virtually always care about. However,
since the name is supplied to the
Cookie
constructor, you rarely need to
call
setName
. On the other hand,
getName
is used on almost every
cookie received on the server. Since the
getCookies
method of
Http
ServletRequest
returns an array of
Cookie
objects, it is common to
loop down this array, calling
getName
until you have a particular name,
then check the value with
getValue
. For an encapsulation of this pro
cess, see the
getCookieValue
method shown in Listing 8.3.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved