Saturday, October 18, 2025
HomeLanguagesJavajava.net.CookiePolicy Class in Java

java.net.CookiePolicy Class in Java

CookiePolicy implementations decide which cookies should be accepted and which should be rejected. Three pre-defined policy implementations are provided, namely ACCEPT_ALL, ACCEPT_NONE, and ACCEPT_ORIGINAL_SERVER.

Signature

public interface CookiePolicy

Fields

S.NO      

Field                               

Description                                                                                          

Data Type                                          

1. ACCEPT_ALL                     CookiePolicy.ACCEPT_ALL is a predefined policy that accepts all the cookies. static CookiePolicy
2. ACCEPT_ORIGINAL_SERVER                     CookiePolicy.ACCEPT_ORIGINAL_SERVER is a predefined policy that only accepts cookies from the original server. static CookiePolicy
3. ACCEPT_NONE CookiePolicy.ACCEPT_NONE is a predefined policy that accepts no cookies. static CookiePolicy

Method

CookiePolicy Interface consists of only one method named as shouldAccept(URI uri, HttpCookie cookie).

shouldAccept(URI uri , HttpCookie cookie) Method

Syntax:

boolean shouldAccept(URI uri, HttpCookie cookie).

Method Parameter:

shouldAccept() has two parameters which are of URI and HttpCookie type.

Method Return Type:

shouldAccept() has boolean return type and will return true if cookie should be accepted otherwise will return false.

Define Our Own Cookie Policy

To define cookie policy – 

  • Implement the CookiePolicy interface.
  • Define the shouldAccept method of CookiePolicy and program according to our need.

Examples

 1. Java program to set cookieManager cookie policy to accept all the cookies –

Java




import java.net.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of cookieManager
        CookieManager cookieManager = new CookieManager();
        // set cookieManager cookie policy using
        // setCookiePolicy method
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        System.out.println("Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ALL");
    }
}


Output

Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ALL

2. Java program to set cookieManager cookie policy to accept no cookies –

Java




import java.net.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of cookieManager
        CookieManager cookieManager = new CookieManager();
        // set cookieManager cookie policy using
        // setCookiePolicy method
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_NONE);
        System.out.println("Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_NONE");
    }
}


Output

Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_NONE

3. Java program to set cookieManager cookie policy to accept cookies from the original server only – 

Java




import java.net.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of cookieManager
        CookieManager cookieManager = new CookieManager();
        // set cookieManager cookie policy using
        // setCookiePolicy method
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
        System.out.println("Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ORIGINAL_SERVER");
    }
}


Output

Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ORIGINAL_SERVER
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS