Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

java.net.CookiePolicy Class in Java

  • Last Updated : 26 Nov, 2021

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_NONECookiePolicy.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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!