Bruno Chevalier wrote:
>
> Thanks Rosdi for your help. The problem is that I can't access to this cookie in
I had the same exact problem with setting cookie with jdsk1.0. I wanted
to goto 2.0 but and servletRunner but it 2.0 required a OS upgrade that
would not be compatible with other 3rd party software we run.
So here is what I have come up with for now. I really new to java so
there is a problem with using "StringTokenizer cookieList=null" in the
"getCookie" member function during repeated calls. I haven't been able
to find the member function that resets the list to the head of the
list, so you might have to reinitialized the class for each cookie
you want, however if you know the sequence of the cookies
just get them in the same order. Anyhow here it is , good luck. Also I
still have a lot of System.print for debugging purposes
James Tuan
//
***************************************************************************
// Usage
//
***************************************************************************
// Setting a cookie
myCookie cookie = new myCookie();
String header = "text/html ";
header += cookie.setCookie("CUSER_ID", PersonId,null);
header += cookie.setCookie("CEVENT_ID", "0",null);
header += cookie.setCookie("CGIFT_ID", "0",null);
header += cookie.setCookie("CATTENDEES_ID", "0",null);
header += cookie.setCookie("CFAMILY_ID", "0",null);
res.setContentType(header);
// Getting a cookie value
myCookie cookie = new myCookie();
cookie.parseCookie(req);
String PersonId = cookie.getCookie("CUSER_ID");
//
***************************************************************************
// myCookie.java
//
***************************************************************************
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.IndexOutOfBoundsException;
import java.util.*;
public class myCookie {
StringTokenizer cookieList=null ;
//
***************************************************************************
public String setCookie(String cookieName, String cookieValue, String
todate)
{
// todate in of the form of Fri, 31-Dec-2012 23:59:59 GMT
String temp = "";
if ( todate != null ) {
temp = "\nSet-cookie: "+ cookieName + "=" + cookieValue
+ ";expires=" + todate + "; path=/ ";
}
else{
temp = "\nSet-cookie: "+ cookieName+ "="+ cookieValue +
"; path=/ ";
}
return (temp);
}
//
***************************************************************************
public String getCookie ( String cookieName){
try {
if ( cookieList != null ) {
while ( cookieList.hasMoreTokens() ){
StringTokenizer valuePairs = new StringTokenizer(
cookieList.nextToken(),"=" );
String NameValue = valuePairs.nextToken();
NameValue = NameValue.trim();
if ( NameValue.compareTo(cookieName) == 0 ) {
String value = (String) valuePairs.nextToken();
System.out.println(" FOUND returning = '" + value +
"'");
return (value);
}
}
}
return (null);
}
catch (Exception ex ){
return(null);
}
}
//
***************************************************************************
public void parseCookie(HttpServletRequest req)
{
String key;
String value;
try {
String myCookieString = req.getHeader("Cookie");
System.out.println("CookieString= " + myCookieString);
if ( myCookieString != null ){
cookieList = new StringTokenizer( myCookieString,";" );
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//
***************************************************************************
public void sendCookie( HttpServletResponse res, String
cookieName,String cookieValue,
String cookieExpDate)
{
String header = "text/html ";
header += setCookie(cookieName, cookieValue,cookieExpDate);
res.setContentType(header);
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html