The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
You can set and retrieve cookies in your rich Internet application (RIA). Cookies can enhance the capabilities of your RIA. For example, consider the scenario where you have applets on various web pages. An applet on a web page cannot directly access or share information with an applet on another web page. In this scenario, cookies provide an important connection between applets and help one applet pass information to another applet on a different web page. Java Web Start applications can also use cookies to store information on the client.
The Cookie Applet example has a CookieAccessor
class that retrieves and sets cookies.
The following code snippet shows the getCookieUsingCookieHandler
method of the CookieAccessor
class:
public void getCookieUsingCookieHandler() { try { // Instantiate CookieManager; // make sure to set CookiePolicy CookieManager manager = new CookieManager(); manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(manager); // get content from URLConnection; // cookies are set by web site URL url = new URL("http://host.example.com"); URLConnection connection = url.openConnection(); connection.getContent(); // get cookies from underlying // CookieStore CookieStore cookieJar = manager.getCookieStore(); List <HttpCookie> cookies = cookieJar.getCookies(); for (HttpCookie cookie: cookies) { System.out.println("CookieHandler retrieved cookie: " + cookie); } } catch(Exception e) { System.out.println("Unable to get cookie using CookieHandler"); e.printStackTrace(); } }
The CookieManager
class is the main entry point for cookie management. Create an instance of the CookieManager
class and set its CookiePolicy
. Set this instance of the CookieManager
as the default CookieHandler
.
Open a URLConnection
to the website of your choice.
Next, retrieve cookies from the underlying CookieStore
by using the getCookies
method.
The following code snippet shows the setCookieUsingCookieHandler
method of the CookieAccessor
class:
public void setCookieUsingCookieHandler() { try { // instantiate CookieManager CookieManager manager = new CookieManager(); CookieHandler.setDefault(manager); CookieStore cookieJar = manager.getCookieStore(); // create cookie HttpCookie cookie = new HttpCookie("UserName", "John Doe"); // add cookie to CookieStore for a // particular URL URL url = new URL("http://host.example.com"); cookieJar.add(url.toURI(), cookie); System.out.println("Added cookie using cookie handler"); } catch(Exception e) { System.out.println("Unable to set cookie using CookieHandler"); e.printStackTrace(); } }
As shown in Retrieving Cookies, the CookieManager
class is the main entry point for cookie management. Create an instance of the CookieManager
class and set the instance as the default CookieHandler
.
Create the desired HttpCookie
with the necessary information. In our example, we have created a new HttpCookie
that sets the UserName
as John Doe
.
Next, add the cookie to the underlying cookie store.
To access cookies, you must sign your RIA JAR file and request permission to run outside of the security sandbox. See the documentation for the jarsigner
tool to learn how to sign JAR files. See Security in Rich Internet Applicationsfor information on requesting permissions.
Download source code for the Cookie Applet example to experiment further.