110 likes | 359 Views
Cookie class. 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? be aware, and use them sparingly. Cookie class. ConstructorCookie(
E N D
1. Cookie class In package javax.servlet.http
Encapsulates a cookie
A cookies value can uniquely identify a client ? good for maintaining state (session management)
2. Cookie class 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
? be aware, and use them sparingly
3. Cookie class Constructor
Cookie( String name, String value )
Cookie class has accessors (get methods) and mutators (set methods) for all the instance variables: name, value, maxAge, comment, domain, path, secure, version
4. Cookie class String getName( )
String getValue( )
void setName( String name )
void setValue( String value )
5. Cookie class void setMaxAge( int expiry )
/* sets the maximum age of the cookie in seconds */
int getMaxAge( )
/* Returns the maximum age of the cookie, in seconds. -1 indicating the cookie will persist until browser shutdown (default or about to expire) */
6. Cookie class void setPath( String path )
/* sets path so that JSPs and servlets from that directory and below can access the cookie; / indicates entire application */
String getPath( )
7. Cookie class void setDomain( String domainPattern )
/* enables to make cookie available to other hosts: if argument is .ct366.com, then cookie is available to www.xyz.ct366.com */
String getDomain( )
8. Cookie class void setSecure( boolean secure )
/* true ? cookie can only be sent over a secure connection; default = false */
boolean isSecure( )
9. Setting a Cookie Cookie cookie = new Cookie( name, value );
// response: implicit HttpServletResponse object
// HttpServletResponse has an addCookie method
void addCookie( Cookie cookie )
response.addCookie( cookie );
/* Typically, cookies are set at the beginning of a JSP page, as they are sent out as part of the HTTP headers. addCookie can be called several times */
10. Forms - Java Server Pages request is an implicit object that we use for retrieving form information
request is an instance of class HttpServletRequest
Information can be found at
http://java.sun.com/webservices/docs/1.5/api/javax/servlet/http/HttpServletRequest.html
11. HttpServletRequest methods getCookies() Returns an array containing all of the Cookie objects the client sent with this request.
// Returns an array of Cookie objects