eHow launches Android app: Get the best of eHow on the go.

How To

How to Remove Cookies in Java

Contributor
By eHow Contributing Writer
(0 Ratings)

Cookies are a handy way for programmers to place settings and information on a user's browser. Cookies save information such as a username, id, or other information that is unique to the user and used by the application. For instance, the programmer can place a cookie on a user's browser to save the username, so the application automatically fills in textboxes as the reader browses the site. When saving important information, for security reasons, it's prudent to delete the cookie when the user logs out.

Difficulty: Moderate
Instructions
  1. 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);

  2. 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);

  3. 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();

  4. 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);

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

eHow Computers
eHow_eHow Technology and Electronics