Results 1 to 19 of 19
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default Applet making cookies

    Hello.

    I have made an applet like test which has questions on which user answers and they got recorded to the database one by one when he click submit button.

    I would like to make cookies to control user (to remember his action if he, for example uses refresh page) and not to allow him to does questions again (which he already did) and to write to database again.

    I would also like to check his previous access to test (that is the applet) and not to allow him to run applet if he has already run it before.

    Can anybody help me how to make cookies from my applet that can do all this?

    I would be very grateful.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Yes, it is.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I'm not sure what exactly your design do. But I'm not recommend you to use cookies, due to many reasons. Major threat is with the security, and also it's not possible to send more data. And also in some browsers, users may disable it, and so on.

    You can disable the navigation bar in the browser. Or you can flag the last user activity and based on that redirect to the next state. Is that make sense to you?

  5. #5
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    When it starts, my applet has one question. User answers in text field and presses submit button. Then he clicks next button and he then goes to the next question. Next button works having counter in it that controls which question will be shown. I coded it like this:

    If (counter==1){//write question one} else if.....

    I hope I have succeed to explain my point.

    Quote Originally Posted by Eranga View Post
    Or you can flag the last user activity and based on that redirect to the next state.
    This sounds like what I need. How can I do that?

    And thank you

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Yes, 4 questions.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Then It's simple actually.

    In a table you can keep five columns actually. In each login create a session for a user, randomly generated number is much better. Then each question submit update the relevant column of the table. At the same time you've to check previous question has been answered or not. If yes simply you can forward it to the next question.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post

    In each login create a session for a user, randomly generated number is much better. Then each question submit update the relevant column of the table. At the same time you've to check previous question has been answered or not. If yes simply you can forward it to the next question.
    Ok, can you explain me that part about creating a session and about randomly generated number? I have never did that before.

    I do make update of database record like you said and that is how I record those 4 answers in one row of table (one record). I suppose it means that is good, but I need help about that checking if the previous question has been answered or not.

    Thank you for interesting in my problem.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    About the session. Just forget about the word for a min. Once a user take your question how did you find who is he/she. You need kind of an identification. You cannot use names it's not practical, since it's not unique. So you need kind of an ID a unique one. Sessions are used for that, simply a lengthy string of letters and numeric. Simplest option you've is generate a random number of 15~20 numbers. Then there is very small probability to duplicate the same.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    About tracking the previous questions, think about a simple logic. Only if a user answer the question only update the relevant column. If not the relevant field should be a null.

  13. #13
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    About session: I understand problem with full name, well, I thought putting simple auto increment id in database... Can you give me a simple example about that random number generating and demonstrate use of that? I know that I ask much, but it will be very helpful for me to see how it works.

    About tracking the previous questions: do you mean that I should put that logic in database? I hope I have understood well.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You've to do something like this.

    Java Code:
    Random randomGenerator = new Random();
    int iNum = randomGenerator.nextInt(10000);
    About the track of question numbers, yes you can build the logic either in DB or withing your code. DB seems more effective since it's not a bulky process.

  15. #15
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    How can I implement this random numbers logic in having session for one specific user and I am confused about how can I check according to this generator if he has been earlier on specific applet?

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Once user login simply pass the number, may be as a URL parameter and so on. What you mean by specific applet?

  17. #17
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I need logic to "know" how many times did he accessed applet and if he left it undone last time, when he runs applet again, he should go to that question on which he left last time. All that is to limit bad recording to database. And also, he can not do refresh page and run applet again from the beginning.

    Where should I pass that random generated number? To database?

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I'm not sure that you have an idea what's really want to do. Just think if you want to track how many times a user login to your application, there MUST be unique way to identify it. You cannot use the name. So what you should do? Each time you have to get an ID from the users, must be a unique.

    Do you really what's mean by a random number? Each time it's changed. I advice you to use it only for an instance as a replacement for the sessions. Because it your case it's really no need to use real sessions.

    What you have to do is, in each login you've to use an ID, a unique one. If users have to login to your application user name and the password, then in your record table there must be an ID as well.


  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I told you that until user answer a specific question leave the relevant cell as a null value. So using that you can simply track the next question that user has to take on after login again. Simply change the design of your record table to count number of participation from a single user, add another column and increment it value in each login.

Similar Threads

  1. Code to make an Applet that downloads?
    By Leeky in forum Java Applets
    Replies: 0
    Last Post: 09-05-2009, 10:56 PM
  2. CooKies and Session
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-27-2009, 01:47 PM
  3. Make java applet a button
    By hervey in forum New To Java
    Replies: 31
    Last Post: 10-30-2008, 05:44 AM
  4. cookies
    By lukky in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 09-02-2008, 07:46 PM
  5. JSP cookies example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 03:11 PM

Posting Permissions

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