Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
8.3 The Servlet Cookie API
183
8.3 The Servlet Cookie API
To send cookies to the client, a servlet should create one or more cookies with
designated names and values with 
new Cookie(name, value)
, set any
optional attributes with 
cookie.setXxx
 (readable later by 
cookie.getXxx
),
and insert the cookies into the response headers with
response.addCookie(cookie)
. To read incoming cookies, a servlet should
call 
request.getCookies
, which returns an array of 
Cookie
 objects corre 
sponding to the cookies the browser has associated with your site (this is 
null
 if
there are no cookies in the request). In most cases, the servlet loops down this
array until it finds the one whose name (
getName
) matches the name it had in
mind, then calls 
getValue
 on that 
Cookie
 to see the value associated with that
name. Each of these topics is discussed in more detail in the following sections.
Creating Cookies
You create a cookie by calling the 
Cookie
 constructor, which takes two
strings: the cookie name and the cookie value. Neither the name nor the
value should contain white space or any of the following characters:
 [ ] ( ) = , " / ? @ : ;
Cookie Attributes
Before adding the cookie to the outgoing headers, you can set various charac 
teristics of the cookie by using one of the following 
setXxx
 methods, where
Xxx
 is the name of the attribute you want to specify. Each 
setXxx
 method
has a corresponding 
getXxx
 method to retrieve the attribute value. Except
for name and value, the cookie attributes apply only to outgoing cookies from
the server to the client; they aren't set on cookies that come from the browser
to the server. See Appendix A (Servlet and JSP Quick Reference) for a sum 
marized version of this information.
public String getComment()
public void setComment(String comment)
These methods look up or specify a comment associated with the 
cookie. With version 0 cookies (see the upcoming subsection on 
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




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

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