Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cookies  RSS feed

 
Mary Cushing
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
Speaking of cookies...
Does anyone know of a routine that lets you add and retrieve a cookie that holds more than one name/value pair. I wrote stringIt and unstringIt methods to name a cookie and for the value use instead a list of N/Vs in this format name:value|name:value| etc etc
I probably am reinventing the wheel here but I could not find an example anywhere to use just one cookie. I know how to do it in javascript but has anyone done it in java?
Thanks.
 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add your name/value pairs to a collection, anad then put your collection in a cookie.

Bosun
 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding my previous post, since the name/value of a cookie must be strings, you may have to resort to using the HttpSession interface.

Bosun
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bosun, do you mean "don't use cookies, use the session object"? or something else?

 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Mike that's what I meant. My thinking is that what he is trying to achieve might be easier by using a session object instead of cookies. Since he can add his name/value pairs to a hashMap or so, and then store it in the session.

Bosun

 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.. boys named Mary. What's it coming to?



But how about that? If you want to persist it beyond a session, you need to use a cookie. And the Cookie class only takes String values.

The only way I see is to do as Mary has done, ie: Make your own 'markup' within a string that allows you to delimit multiple values.
 
Mary Cushing
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Bosun and Mike, for your comments.
I am new to java and was trying to figure out if one the collection objects has methods that I can use. Rather than doing it myself, I use a two dimensional array, BTW. Do any of them have methods that 'translate' them to a string or in the reverse, take a string and convert it to a collection. This would make it easier for me to do multiple cookies. (I do need them to persist.) I was hoping for something like javascript's split method or some other method that takes delimited string and converts it to an array or other collection that I can manipulate easily.
I hope you understand what I getting at.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can look at StringTokenizer class. It's in the java.util package. The Tokenizer will take a String in it's constructor and has a few methods that will return each 'token' of that String.

What determines the delimiter of your tokens is either the defaults (space, tab, newline, return and form feed), or the second String you send to the Tokenizer constructor.

You would use this class for very simple, custom-made Strings of values. (like you have with the colons and such).

All objects in Java have a method they inherit from the Object class... it's called toString(). Most default implementations of this method will provide something like a hex value and it's pretty useless. But others provide useful toString()'s... for example, a HashMap's toString() will return a nicely formatted string of the form... "{name=value[,name=value]}"
[This message has been edited by Mike Curwen (edited October 28, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!