W3C home > Mailing lists > Public > www-jigsaw@w3.org > November to December 1996

set/get Cookies from PostableResource

From: Steve <daddyo@sparky.oroad.com>
Date: Thu, 14 Nov 1996 13:53:47 -0800 (PST)
Message-Id: <199611142153.NAA20948@sparky.oroad.com>
To: www-jigsaw@w3.org
I've be working with the PostableResource (thanks to everyone who replied to
my resource 'caching' question - A: bug in JDK) quite a bit, and have been
trying to set and get cookies.

For example, I want to set a cookie id=23423 in one script and then get the
id in a different script. I've created a class CGIApplet (couldn't think of
a good name) that extends PostableResource. Its intended purpose is to have
a bunch of commonly used methods such as getCookie() and setCookie().

I keep getting a NullPointerException when I call setCookie() in the code
below. It seems to come from the addCookie() method of HttpCookieList. 

Does anyone have any simple code that sets a cookie? 


// start code -----------------------------------------

package oroad.jigsaw.util;

import w3c.www.http.*;
import w3c.jigsaw.http.*;
import w3c.jigsaw.forms.*;
import w3c.jigsaw.html.*;

/**
 * CGIApplet adds some commonly used functions to the web servers
 * java API. It also attempts to encapsulate the API specifics of the 
 * server somewhat. However, due to wide variance in server architectures, 
 * this isn't terribly feasible. This particular API is 
 * Jigsaw specific since two of its methods need a Reply object.
 *
 * @version 0.6
 * @author spetschu@oroad.com
 */


public class CGIApplet extends PostableResource {

	public CGIApplet() { }

	/**
	 * Set the http cookie for a given name and value.
	 *
	 * @param Expects a Reply object to modify, and a key-value pair.
	 * @return
			HttpCookieList hcl = reply.getCookie();
			/**** add this check for null, 
			 **** didn't seem to help ***/
			if (hcl == null) {
				hcl = new HttpCookieList();
				/**** problem here ****/
				hcl.addCookie(keyName, valueName);
				reply.setCookie(hcl);
			}
			else {
				hcl.addCookie(keyName, valueName);
				reply.setCookie(hcl);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * Get the specified http cookie.
	 *
	 * @param Expects a Reply to modify, and a key-value pair.
	 * @return A String representing the value.
	 * @exception
	 */
	public String getCookie(Reply reply, String key) {
		HttpCookieList hcl = reply.getCookie();
		HttpCookie c = hcl.getCookie(key);
		return c.getValue();
	}

}
// -----------------------------------
Received on Thursday, 14 November 1996 16:53:26 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 9 April 2012 12:13:25 GMT