java.net.CookiePolicy Class in Java Last Updated : 26 Nov, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Signaturepublic interface CookiePolicyFields S.NO Field Description Data Type 1.ACCEPT_ALL CookiePolicy.ACCEPT_ALL is a predefined policy that accepts all the cookies.static CookiePolicy2.ACCEPT_ORIGINAL_SERVER CookiePolicy.ACCEPT_ORIGINAL_SERVER is a predefined policy that only accepts cookies from the original server.static CookiePolicy3.ACCEPT_NONECookiePolicy.ACCEPT_NONE is a predefined policy that accepts no cookies.static CookiePolicyMethod 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"); } } OutputCookie 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"); } } OutputCookie 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"); } } OutputCookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ORIGINAL_SERVER Comment More infoAdvertise with us Next Article java.net.CookieManager Class in Java H harshsethi2000 Follow Improve Article Tags : Java Java-Classes Java-net-package Practice Tags : Java Similar Reads java.net.CookieStore Class in Java 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 Cook 4 min read java.net.CookieHandler Class in Java The object of the CookieHandler Class in Java provides a callback mechanism for hooking up an HTTP state management policy implementation into the HTTP protocol handler. The mechanism of how to make HTTP requests and responses is specified by the HTTP state management mechanism. A system-wide Cookie 2 min read java.net.CookieManager Class in Java The CookieManager class provides a precise implementation of CookieHandler. This separates the storage of cookies from the policy surrounding accepting and rejecting cookies. A CookieManager is initialized with a CookieStore and a CookiePolicy. The CookieStore manages storage, and the CookiePolicy o 4 min read Javax.servlet.http.Cookie class in Java Many websites use small strings of text known as cookies to store persistent client-side state between connections. Cookies are passed from server to client and back again in the HTTP headers of requests and responses. Cookies can be used by a server to indicate session IDs, shopping cart contents, 7 min read java.net.URL Class in Java URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part 4 min read java.net.Proxy Class in Java A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are 3 min read Like