-
Step 1
Create the cookie. You first need an instance of a cookie before you are able to delete it. Referencing a nonexistent cookie throws an error to the compiler. Setup a java cookie with the following code.
Cookie getUser = new Cookie("UserId", userId); -
Step 2
Set comments in the cookie. Step 1 provides a cookie instance that can be called throughout the code. The cookie is placed on the machine, so now the code needs to use it. The following line of code sets a comment in the cookie that can be used to print out settings on the browser.
getUser.setComment("This user's id is" + userId); -
Step 3
Retrieve the value of a cookie. You may want to retrieve a value before you delete the cookie. The following code retrieves the cookie's value before deleting it permanently.
Cookie[] myUserCookies = request.getCookies();
Cookie theUserId = myUserCookies[i];
string saveUserId = theUserId.getValue(); -
Step 4
Delete the cookie. Now that the cookie's value is saved, the following code deletes the cookie. Setting a the maximum age of the cookie to zero deletes it from the user's browser.
theUserId.setMaxAge(0);











