Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
530
Appendix A
Servlet and JSP Quick Reference
A.8 Handling Cookies
Typical Uses of Cookies
Identifying a user during an e commerce session
Avoiding username and password
Customizing a site
Focusing advertising
Problems with Cookies
It's a privacy problem, not a security problem.
Privacy problems include: servers can remember what you did in
previous sessions; if you give out personal information, servers can link
that information to your previous actions; servers can share cookie
information through use of a cooperating third party like
doubleclick.net (by each loading image off the third party site); poorly
designed sites could store sensitive information like credit card
numbers directly in the cookie.
General Usage
Sending cookie to browser (standard approach):
Cookie c = new Cookie("name", "value");
c.setMaxAge(...);
// Set other attributes.
response.addCookie(c);
Sending cookie to browser (simplified approach):
Use
LongLivedCookie
class (Section 8.5).
Reading cookies from browser (standard approach):
Cookie[] cookies = response.getCookies();
for(int i=0; i
Cookie c = cookies[i];
if (c.getName().equals("someName")) {
doSomethingWith(c);
break;
}
}
Reading cookies from browser (simplified approach):
Extract cookie or cookie value from cookie array by using
ServletUtilities.getCookie
or
ServletUtilities.getCookieValue
.
Cookie Methods
getComment/setComment: gets/sets comment. Not supported in
version 0 cookies (which is what most browsers now support).
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