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.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
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 web site of your choice. In the example, the cookies
that are set by the web site wwww.sun.com
are retrieved.
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://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
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.
jarsigner
tool to learn how to sign JAR files.
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 ...
Download source code for the Cookie Applet example to experiment further.