Cookie Parse Read Problem (newbie)

Discussion in 'Java' started by petek, Jan 24, 2004.

  1. petek

    petek Guest

    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!
     
    petek, Jan 24, 2004
    #1
    1. Advertising

  2. petek

    petek Guest

    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!


    On Sat, 24 Jan 2004 04:52:41 -0700, petek <>
    wrote:

    >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!
    >
     
    petek, Jan 24, 2004
    #2
    1. Advertising

  3. petek

    Andrew Hobbs Guest

    "petek" <> wrote in message
    news:...
    > Found the solution...
    >
    > Instead of StringTokenizer, I used:
    >

    ..
    ..(snip)
    ..
    > >}
    > >
    > >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


    This is exactly what I would expect.

    > >
    > >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 ...


    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.


    > >
    > >Please, any help is greatly appreciated. Thank you!
    > >

    >
    >
     
    Andrew Hobbs, Jan 24, 2004
    #3
  4. petek

    sowbug Guest

    My friend, javascript and java have virtually nothing in common.


    petek <> wrote in message news:<>...
    > 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!
     
    sowbug, Jan 24, 2004
    #4
  5. petek

    sowbug Guest

    import idiot.*;

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




    petek <> wrote in message news:<>...
    > 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!
     
    sowbug, Jan 24, 2004
    #5
  6. "sowbug" ...
    ....
    | System.out.println("See that it's java!");

    ;-)
     
    Andrew Thompson, Jan 24, 2004
    #6
  7. petek

    NOBODY Guest

    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






    petek <> wrote in
    news::

    > 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!
    >
    >
    >
     
    NOBODY, Jan 24, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    19
    Views:
    1,159
    Daniel Vallstrom
    Mar 15, 2005
  2. Bill Kelly

    CGI::Cookie.parse problem?

    Bill Kelly, Jan 25, 2006, in forum: Ruby
    Replies:
    0
    Views:
    135
    Bill Kelly
    Jan 25, 2006
  3. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    201
    7stud --
    Feb 20, 2008
  4. Ryan Mulligan

    Is CGI::Cookie::parse terribly broken?

    Ryan Mulligan, Jul 23, 2008, in forum: Ruby
    Replies:
    0
    Views:
    256
    Ryan Mulligan
    Jul 23, 2008
  5. emilgo

    Read cookie with different cookie

    emilgo, Feb 13, 2004, in forum: Javascript
    Replies:
    0
    Views:
    97
    emilgo
    Feb 13, 2004
Loading...

Share This Page