Skip to content
Related Articles

Related Articles Class in Java

View Discussion
Improve Article
Save Article
  • Last Updated : 15 Dec, 2021

A CookieStore is an interface in Java that is a storage area for cookies. It is used to store and retrieve cookies. A CookieStore is responsible for removing HTTPCookie instances that have expired.

The CookieManager adds the cookies to the CookieStore for every incoming HTTP response by calling CookieStore.add() and retrieves the cookies from the CookieStore for every outgoing HTTP request by calling CookieStore.get().

The methods inside the CookieStore interface are shown in the table below.

Method Action Performed
add(URI uri, HttpCookie cookie) Adds one HTTP cookie to the store.
get(URI uri) Retrieves cookies whose domain matches the URI.
getCookies() Get all cookies in CookieStore which are not expired.
getURIs() Get all URIs that identify cookies in CookieStore
remove(URI uri, HttpCookie cookie) Removes a cookie from CookieStore
removeAll() Removes all cookies in the CookieStore

Method 1: add(URI uri, HttpCookie cookie)

The add(URI uri, HttpCookie cookie) method adds one HTTP cookie to the store. This method is called for every incoming HTTP response. If a cookie corresponding to the given URI already exists, it is replaced with the new one.


public void add(URI uri, HttpCookie cookie)

Method 2: get(URI uri)

The get(URI uri) method retrieves cookies whose domain matches the URI. This method is called for every outgoing HTTP request. It returns an immutable list of HTTP cookies that are not expired. If no cookie matches the URI, it returns an empty list. If the parameter URI is passed as null, then it throws an exception called Null Pointer Exception.


public List<HttpCookie> get(URI uri)

Method 3: getCookies(

The getCookies() method retrieves all the cookies from the CookieStore, which are not expired. It returns an immutable list of HTTP cookies. If there is no cookie in the CookieStore, it returns an empty list.


public List<HttpCookie> getCookies()

Method 4: getURIs()

The getURIs() method retrieves all the URIs which identify the cookies in the CookieStore and returns an immutable list of URIs. It returns an empty list if no cookie in the CookieStore is associated with the URI.


public List<URI uri> getURIs()

Method 5: remove(URI uri, HttpCookie cookie)

The remove(URI uri, HttpCookie cookie) method removes a cookie in the CookieStore whose URI is associated with the given one. If the cookie to be removed is not related to the URI when added, then URI is passed as null. If the cookie is passed as null, it throws a Null Pointer Exception. If the cookie is successfully removed, then it returns a Boolean value true.


public boolean remove(URI uri, HttpCookie cookie)

Method 6: removeAll()

The removeAll() method removes all the cookies in the CookieStore. If all the cookies are successfully removed, then it returns a Boolean value true.


public boolean removeAll()



import java.util.*;
public class GFG {
    public static void main(String[] args)
        // CookieManager and CookieStore
        CookieManager cookieManager = new CookieManager();
        CookieStore cookieStore
            = cookieManager.getCookieStore();
        // creating cookies and URI
        HttpCookie cookieA = new HttpCookie("First", "1");
        HttpCookie cookieB = new HttpCookie("Second", "2");
        URI uri
            = URI.create("");
        // Method 1 - add(URI uri, HttpCookie cookie)
        cookieStore.add(uri, cookieA);
        cookieStore.add(null, cookieB);
            "Cookies successfully added\n");
        // Method 2 - get(URI uri)
        List cookiesWithURI = cookieStore.get(uri);
            "Cookies associated with URI in CookieStore : "
            + cookiesWithURI + "\n");
        // Method 3 - getCookies()
        List cookieList = cookieStore.getCookies();
        System.out.println("Cookies in CookieStore : "
                           + cookieList + "\n");
        // Method 4 - getURIs()
        List uriList = cookieStore.getURIs();
        System.out.println("URIs in CookieStore" + uriList
                           + "\n");
        // Method 5 - remove(URI uri, HttpCookie cookie)
            "Removal of Cookie : "
            + cookieStore.remove(uri, cookieA));
        List remainingCookieList = cookieStore.getCookies();
            "Remaining Cookies : " + cookieList + "\n");
        // Method 6 - removeAll()
        System.out.println("Removal of all Cookies : "
                           + cookieStore.removeAll());
        List EmptyCookieList = cookieStore.getCookies();
            "Empty CookieStore : " + cookieList);


Cookies successfully added

Cookies associated with URI in CookieStore : [First="1"]

Cookies in CookieStore : [First="1", Second="2"]

URIs in CookieStore[]

Removal of Cookie : true
Remaining Cookies : [Second="2"]

Removal of all Cookies : true
Empty CookieStore : []

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!