Online Java Compiler By
JavaTpoint.com
import java.net.HttpCookie; public class JavaHttpCookieExample2 { public static void main(String[] args) { HttpCookie cookie = new HttpCookie("Tutorial", "1"); //Specifies whether the user agent can discard the cookie unconditionally or not. cookie.setDiscard(true); //Returns the discard attribute of the cookie. System.out.println("The discard attribute is given as: "+cookie.getDiscard()); //Sets the version of the cookie protocol. cookie.setVersion(1); //Returns the version of the protocol which the cookie compiles with. System.out.println("The version is given as: "+ cookie.getVersion()); //Assigns a new value to a cookie after the cookie creation. cookie.setValue("Hello"); //Returns the value of the cookie. System.out.println("The value is given as: "+cookie.getValue()); //Reports whether the HTTP cookie has expired or not. System.out.println("Check whether cookie has expired or not: "+ cookie.hasExpired()); //Indicates whether the cookie can be considered HTTP Only. cookie.setHttpOnly(true); //Returns true if the cookie contains HttpOnly attribute. System.out.println("Check whether the cookie contains the HttpOnly attribute or not: " +cookie.isHttpOnly()); } }
Output