R
Roedy Green
I wrote this little Applet for the cookie entry in the java glossary.
It dies unable to get a cookieHandler.
I have three questions:
1. why does it insist or me signing the Applet to call getDefault?
Surely unsigned Applets are allowed to know their cookies.
2. What do I have to do to get a non-null CookieHandler?
3. Just what sort of thing does get want in the RequestHeaders map?
4. If this did work, what do I do to get some cookies piggybacked on
my HTTP messages to the server. Does the browser do that transparently
once cookies are in the store?
I understand that you are not allowed to look at cookies other than
from you own website. My own website does not use cookies, so I can't
easily experiment.
<sscee>
/**
* Test ability of Applet to look at cookies
*/
package com.mindprod.example;
import java.applet.Applet;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Test ability of Applet to look at cookies
*
* @author Roedy Green
*/
public class TestCookie extends Applet {
/**
* standard applet Init
*/
public void init ()
{
System.out.println( "init called" );
// fetch cookies from the Browser's cache of cookies
// Requires signed Applet permission
CookieHandler ch = CookieHandler.getDefault();
if ( ch == null )
{
System.err.println( "no cookie handler" );
}
URI uri = null;
try
{
uri = new URI( "http://mindprod.com" );
}
catch ( URISyntaxException e )
{
System.err.println( e.getMessage() );
}
Map<String, List<String>> requestHeaders = new HashMap<String,
List<String>>(
200 );
// I'm not sure what you are supposed to put in the
requestHeaders
// resquestHeaders.put( String, ArrayList<String>) of some
sort.
Map<String, List<String>> results = null;
try
{
// returns an immutable map of cookies
results = ch.get( uri, requestHeaders );
}
catch ( IOException e )
{
System.err.println( e.getMessage() );
}
// display the results, keys Cookie1 and Cookie2
for ( String key : results.keySet() )
{
System.out.println( key );
for ( String item : results.get( key ) )
{
System.out.println( " " + item );
}
}
}
}
</sscce>
It dies unable to get a cookieHandler.
I have three questions:
1. why does it insist or me signing the Applet to call getDefault?
Surely unsigned Applets are allowed to know their cookies.
2. What do I have to do to get a non-null CookieHandler?
3. Just what sort of thing does get want in the RequestHeaders map?
4. If this did work, what do I do to get some cookies piggybacked on
my HTTP messages to the server. Does the browser do that transparently
once cookies are in the store?
I understand that you are not allowed to look at cookies other than
from you own website. My own website does not use cookies, so I can't
easily experiment.
<sscee>
/**
* Test ability of Applet to look at cookies
*/
package com.mindprod.example;
import java.applet.Applet;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Test ability of Applet to look at cookies
*
* @author Roedy Green
*/
public class TestCookie extends Applet {
/**
* standard applet Init
*/
public void init ()
{
System.out.println( "init called" );
// fetch cookies from the Browser's cache of cookies
// Requires signed Applet permission
CookieHandler ch = CookieHandler.getDefault();
if ( ch == null )
{
System.err.println( "no cookie handler" );
}
URI uri = null;
try
{
uri = new URI( "http://mindprod.com" );
}
catch ( URISyntaxException e )
{
System.err.println( e.getMessage() );
}
Map<String, List<String>> requestHeaders = new HashMap<String,
List<String>>(
200 );
// I'm not sure what you are supposed to put in the
requestHeaders
// resquestHeaders.put( String, ArrayList<String>) of some
sort.
Map<String, List<String>> results = null;
try
{
// returns an immutable map of cookies
results = ch.get( uri, requestHeaders );
}
catch ( IOException e )
{
System.err.println( e.getMessage() );
}
// display the results, keys Cookie1 and Cookie2
for ( String key : results.keySet() )
{
System.out.println( key );
for ( String item : results.get( key ) )
{
System.out.println( " " + item );
}
}
}
}
</sscce>