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
CookieAccessorclass that retrieves and sets cookies.Retrieving Cookies
The following code snippet shows the
getCookieUsingCookieHandlermethod of theCookieAccessorclass: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://www.sun.com"); URLConnection connection = url.openConnection(); connection.getContent(); // get cookies from underlying CookieStore CookieStore cookieJar = manager.getCookieStore(); Listcookies = 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
CookieManagerclass is the main entry point for cookie management. Create an instance of theCookieManagerclass and set itsCookiePolicy. Set this instance of theCookieManageras the defaultCookieHandler.Open a
URLConnectionto the web site of your choice. In the example, the cookies that are set by the web sitewwww.sun.comare retrieved.Next, retrieve cookies from the underlying
CookieStoreby using thegetCookiesmethod.Setting Cookies
The following code snippet shows the
setCookieUsingCookieHandlermethod of theCookieAccessorclass: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://www.sun.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
CookieManagerclass is the main entry point for cookie management. Create an instance of theCookieManagerclass and set the instance as the defaultCookieHandler.Create the desired
HttpCookiewith the necessary information. In our example, we have created a newHttpCookiethat sets theUserNameasJohn Doe.Next, add the cookie to the underlying cookie store.
Note: You must sign your RIA JAR file in order to access cookies. See the documentation for thejarsignertool to learn how to sign JAR files.Running the Cookie Applet Example
Open
AppletPage.htmlin a browser to run the Cookie Applet example. Check the Java Console log for details of cookies that have been set and retrieved. You should see the following output in the Java Console log (session details vary).---- Access cookies using CookieHandler --- CookieHandler retrieved cookie: JSESSIONID=3bc935c18b8d36319be9497fb892 CookieHandler retrieved cookie: JROUTE=eKVJ4oW0NOer888s Added cookie using cookie handler ...
Note: If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 6 update 10 release.
Note: If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.Download source code for the Cookie Applet example to experiment further.