Spec-Zone .ru
спецификации, руководства, описания, API
|
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://www.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
CookieManager
as the default
CookieHandler
Open a
URLConnection
wwww.sun.com
are retrieved.
Next, retrieve cookies from the underlying
CookieStore
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://www.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
CookieManager
class and set the instance as the default
CookieHandler
Create the desired
HttpCookie
HttpCookie
that sets the UserName
as John Doe
.
Next, add the cookie to the underlying cookie store.
jarsigner
Open
AppletPage.html
in 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 ...
If you don't see the applet running, you need to install at least the
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.