Cookies
Cookies
Method Description
Sets the maximum age of the cookie in
public void setMaxAge(int expiry)
seconds.
Returns the name of the cookie. The
public String getName()
name cannot be changed after creation.
public String getValue() Returns the value of the cookie.
public void setName(String name) changes the name of the cookie.
public void setValue(String value) changes the value of the cookie.
1. Creating Cookies
Three steps to creating a new cookie:
1) Create a new Cookie Object
Cookie cookie = new Cookie (name, value);
2) Set any cookie attributes
cookie.setMaxAge (60);
3) Add your cookie to the response object:
Response.addCookie (cookie)
Cookie Constructor
Create a new cookie by calling the Cookie
constructor and specifying:
Name
Value
Example:
Cookie cookie = new Cookie (“school”, “abc”);
Neither the name nor the value may contain
whitespace or any of the following characters:
[]()=,“/?@;
2. Set Cookie Attributes
Before adding a cookie to the Response
object, you can set attributes.
Attributes include:
Name/Value
Domain
Maximum Age
Path
Version
Cookie Name
public String getName();