Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2008
    Posts
    25
    Rep Power
    0

    Default 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

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    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.

  3. #3
    Join Date
    Mar 2008
    Posts
    25
    Rep Power
    0

    Default

    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

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    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.

  5. #5
    Join Date
    Mar 2008
    Posts
    25
    Rep Power
    0

    Default

    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

  6. #6
    Join Date
    Mar 2008
    Posts
    25
    Rep Power
    0

    Default

    do you have anymore info on this method
    DBUser user = fetchFromDatabaseIfValid(username, password);
    because i need implement req.getSession() now cheers

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    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.

  8. #8
    Join Date
    Mar 2008
    Posts
    25
    Rep Power
    0

    Default

    i seems to have got things working but after i have purchased items and start browsing again OR if i have logged in i get some null pointer exception to this bit of code...sessionArrayGames.add(productCode);
    any ideas why this is happening? cheers

Similar Threads

  1. cookies
    By lukky in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 09-02-2008, 08:46 PM
  2. Store String Tokenizer o/p onto an array
    By rajchief in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 10:00 PM
  3. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  4. JSP cookies example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 04:11 PM
  5. I would then store a string...
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •