Cookie Parse Read Problem (newbie)

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!
 
P

petek

Found the solution...

Instead of StringTokenizer, I used:

String[] result = cookies.getValue().split("%3F");

That command split up the values in the cookie correctly. Although,
still don't know why StringTokenizer didn't work!
 
A

Andrew Hobbs

petek said:
Found the solution...

Instead of StringTokenizer, I used:
..
..(snip)
..

This is exactly what I would expect.

Yes.

If you read the documentation for StringTokenizer you will see that the
second string you provide is a string of separators. ie each character is
recognized as a distinct separator. In your case you have set three
separators "%", "3" and "F". So that for the string "hello%3F3%3FFemale",
every character between 'hello' and 'emale' is considered a separator and
isn't returned.
 
S

sowbug

import idiot.*;

public class QuickDraw {
if(lookAtCode) {
System.out.println("See that it's java!");
}
}
 
N

NOBODY

first, you are trying to tokenize an obviously urlencoded value.
urldecode it first. Then only you may try to tokenize something usefull.
%3f is a question mark "?"

decoded, it becomes: hello?3?Female
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top