Hi Guys, Just started working on servlets.needed help. well i m developing a online exams program. What i need to do is read 40 records from database which i m doing successfully.But when i run on network what happens is when two or more persons start the test if one person answers the first one..the other person gets the second one straightaway.I have a slight idea why this happens..but would like if anyone explains it to me in better way.also what the solution is. Thanks
This kind of problem is almost always because you have member variables declared in your Servlet. You must always remember that the Servlet API is multi-threaded - all client requests use the same servlet, and not store chnageable values in member variables, only in local variables or some context which is not shared between Servlets (such as a Session).
Use local variables as Frank suggests or you can also have you servlet implement SingleThreadModel. The servlet container will then create a new instance of the servlet for each request. That is what I did at my first servlet site. It seemed easier than deciding what code had to be syncronized.