session problem --======urgent plz======--

the problem i have is with session handling. i have a code for online exam.the code works fine for single user. i tried to make the code more efficient by using session object so that multiple clients could access it. i have a question count and the score count in my code. the problem is that i get different session id for different browser windows,but the both the counts are not isolated from each other. i.e if one user has a question count 3 and then second user opens other browser, instesd of getting question count 1 , he is getting question count 4. same is the case for score count.for the first correct answer he is getting the score of first user's score incremented by one and not 1 i want to know how can the question count ans score be different for different users? these counts shoul not have any sort of relation between them. following is the code for my servlet ****** Please let me know what changes shall i make. its very urgent coz i gotto submit the code as my mini project in my college ========================================== import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; import java.util.*; public class Check_ans extends HttpServlet {

but i am calling the same servlet from itself. i.e i am generating the dynamic page and it is calling the same servlet again and again. so if i initialize score=0,count=0 inside doGet the score is incremented but again set to zero in next iteration. if i take count,score as static and public the scores are incrementing but the mentioned problem arise

Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124

posted Apr 24, 2001 13:47:00

0

Originally posted by sachin dabhade:but i am calling the same servlet from itself. i.e i am generating the dynamic page and it is calling the same servlet again and again. so if i initialize score=0,count=0 inside doGet the score is incremented but again set to zero in next iteration.

Well that's what you want, isn't it??? You want to keep the score and count in the session instead of keeping it in the class.. like below?

[This message has been edited by Bhupinder Dhillon (edited April 24, 2001).] [This message has been edited by Bhupinder Dhillon (edited April 24, 2001).]

Sachin, When you declare a variable as static, that means there is only one value for that variable. No matter how many instances of a class exist, they all reference the same value of a static variable. If one instance changes the value, that value changes for all instances. That is what the declaration of static does. However, this has nothing to do with your problem. The only thing you have declared as static that I can see is two strings and a connection. The strings you are using to call session variables (which doesn't make a ton of sense to me) and the connection is just that. (why do you need to declare static final strings to reference your session variables? Anyway. . .) Ok, so your servlet is running, and someone want's to take the test. They start trucking along and then someone else wants to take it too. Now we have a score and a count variable declared as integers in the servlet. These are the variables you are using to print out the score and the count. The way you have this coded, the only thing that can ever happen to score or count is they go up one, no matter what session is making the request. You seem to try to make some sort of check of the session value with the whole i,j stuff but you are only checking to see if these are null or not. So when anyone answers correctly, score gets incremented and then printed out. You appear to be assigning the correct value of score to the session attribute, but then you just print of score, which never gets reassigned for the session request. Redesign your code so that you get and reassign the value for score and count from the session each time. It looks like you thought about sorta trying to do this, with the score = i.intValue() stuff that got commented out. (Why do you want i and j as Integers. . . What is the intValue() for? Seems like extra baggage, like the static final Strings) I suggest stepping through all your logic and you should solve your problem.

[This message has been edited by Andrew Shafer (edited April 24, 2001).]

If you open a new browser window from the same browser, you will still be in the same session. You would have to use somebody elses machine or (?) a different (new) browser program on the same machine (not sure about this last). --Paul

Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124

posted Apr 25, 2001 16:04:00

0

Now consider this...

[This message has been edited by Bhupinder Dhillon (edited April 25, 2001).]

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com