Results 1 to 8 of 8
- 01-09-2009, 04:37 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 25
- Rep Power
- 0
how to store an ArrayList<string> into cookies?
hi, im currently deisgning a shopping basket and have set it up so that during the session it stores the product code and console type in a an arrayList <String> and displays the full details of the game with these two values from the jdbc.
The problem i have is how do you go about storing these as cookies? i have it set up to store the sessionID, productCode, consoleType, userName in session and longLived cookies.
now, after the user has gone through the checkout when storing the cookies from the ArrayList<string> its adds a new set cookies each time for all the values. so if the user has bought 2 items in effect will produc 8 cookies, is this the correct way of doing it? i imagine not because theres is a max number of cookies of 20 i think? so you would set up an array of productCodes, consoleType etc then send them in as Strings to each cookie?
also from this, after the cookies are stored and the user has registered how do you go about retrieving the items releveant to that session and user when they next log in?
any help would be much appreciated cheers
- 01-09-2009, 04:58 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Are you programming a Servlet? Generally, you rarely need to program cookies directly. I would suggest looking at the Servlet Session API (link is to one of my tutorials). The Session API lets you handle associating arbitrary objects (in your case, array lists of strings, for example) with sessions.
Neil Coffey
Javamex - Java tutorials and performance info
- 01-09-2009, 05:20 PM #3
Member
- Join Date
- Mar 2008
- Posts
- 25
- Rep Power
- 0
yes, using servlets through tomcat. just had a quick scan of the link, and it seems i only have to store the session and not individual attributes. cheers, i'll have a play and see if that helps
- 01-09-2009, 05:22 PM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Not only do you not have to, but for security reasons, you actually shouldn't generally store data in a cookie in a form that the user can manipulate (unless you're also planning to build in your own security checks to get round this). The Session API will handle this kind of issue for you.
Neil Coffey
Javamex - Java tutorials and performance info
- 01-09-2009, 05:39 PM #5
Member
- Join Date
- Mar 2008
- Posts
- 25
- Rep Power
- 0
it's an assignment and security isnt really a concern at this stage but best to learn correct methods rather than pick up bad habbits, cheers
- 01-09-2009, 06:37 PM #6
Member
- Join Date
- Mar 2008
- Posts
- 25
- Rep Power
- 0
do you have anymore info on this method
DBUser user = fetchFromDatabaseIfValid(username, password);
because i need implement req.getSession() now cheers
- 01-09-2009, 08:33 PM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Oh, that's just a made up method name: the idea is that in your application, you would have some database of users, and you'd have some method to fetch a user from your database. So you just need to implement such a thing.
If you're not trying to get bogged down in databases for now but just want to get the logic of your shopping cart working, then just create some simple class to represent a user (which in the long run would be a wrapper class around a row in a database), and make a method that, when passed in the correct user name and password, will return an instance of your user object.Neil Coffey
Javamex - Java tutorials and performance info
- 01-12-2009, 12:30 AM #8
Member
- Join Date
- Mar 2008
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
cookies
By lukky in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 09-02-2008, 07:46 PM -
Store String Tokenizer o/p onto an array
By rajchief in forum New To JavaReplies: 1Last Post: 08-08-2008, 09:00 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
JSP cookies example
By Java Tip in forum Java TipReplies: 0Last Post: 01-15-2008, 03:11 PM -
I would then store a string...
By susan in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks