P
petek
I'm having a problem parsing a cookie that was created via javascript.
I create the cookie in javascript, update values, etc.
Sample Cookie value:
cookie name: myCookie
value: hello%3F3%3FFemale
Then I use the following code in JSP:
String cookie1 = "myCookie";
int cookieCount = 0;
Cookie [] cookies = request.getCookies();
for (int i=0; i<cookies.length; i++) {
if (cookies.getName().equals(cookie1)) {
out.println("Cookie name is: " + cookies.getName()
+ "<br>");
out.println("Cookie value is: " +
cookies.getValue() + "<br>");
StringTokenizer st = new
StringTokenizer(cookies.getValue(),"%3F", false);
int cookieLength = st.countTokens();
out.println("The number of tokens in this cookie is: "
+ cookieLength + "<br><br>");
String[] cVuserData = new String[cookieLength];
while (st.hasMoreElements()) {
cVuserData[cookieCount] = st.nextToken();
out.println("The value in token#" +
cookieCount + " is: " + cVuserData[cookieCount] + "<br>");
cookieCount++;
}
}
}
The code finds the cookie no problem but output looks like this:
Cookie name is: myCookie
Cookie value is: hello%3F3%3FFemale
The number of tokens in this cookie is: 2
The value in token#0 is: hello
The value in token#1 is: emale
The problem is that it does not see the second value of "3" and trims
the "F" from Female in the third value. Obviously, any value that
appears in the StringTokenizer separator "%3F" is clipped or ignored.
I need to be able to write those values to a database so it's
imperative that I am able to read the cookie correctly.
Am I using StringTokenizer wrong or do I need to do something else?
Please, any help is greatly appreciated. Thank you!
I create the cookie in javascript, update values, etc.
Sample Cookie value:
cookie name: myCookie
value: hello%3F3%3FFemale
Then I use the following code in JSP:
String cookie1 = "myCookie";
int cookieCount = 0;
Cookie [] cookies = request.getCookies();
for (int i=0; i<cookies.length; i++) {
if (cookies.getName().equals(cookie1)) {
out.println("Cookie name is: " + cookies.getName()
+ "<br>");
out.println("Cookie value is: " +
cookies.getValue() + "<br>");
StringTokenizer st = new
StringTokenizer(cookies.getValue(),"%3F", false);
int cookieLength = st.countTokens();
out.println("The number of tokens in this cookie is: "
+ cookieLength + "<br><br>");
String[] cVuserData = new String[cookieLength];
while (st.hasMoreElements()) {
cVuserData[cookieCount] = st.nextToken();
out.println("The value in token#" +
cookieCount + " is: " + cVuserData[cookieCount] + "<br>");
cookieCount++;
}
}
}
The code finds the cookie no problem but output looks like this:
Cookie name is: myCookie
Cookie value is: hello%3F3%3FFemale
The number of tokens in this cookie is: 2
The value in token#0 is: hello
The value in token#1 is: emale
The problem is that it does not see the second value of "3" and trims
the "F" from Female in the third value. Obviously, any value that
appears in the StringTokenizer separator "%3F" is clipped or ignored.
I need to be able to write those values to a database so it's
imperative that I am able to read the cookie correctly.
Am I using StringTokenizer wrong or do I need to do something else?
Please, any help is greatly appreciated. Thank you!