[ August 28, 2008: Message edited by: vamsikrishna Gunturi ]
You can go back and change your post to add code tags by clicking the .
I don't know much about the generic servlet and the single thread model.
But one thing, its not really good to eat out exceptions.
Just for the sake of curiosity, will you let me know how do you make request to generic servlet.
Thank you for your reply.
Actually i am learning servlets from basics.
Generic servlet is just a wrapper class of class Servlet.
It just works same as any other servlet.Just you need to inlcude it in a package and call the servlet from browser by the name given in the url mapping in web.xml.
when i implemented a singlethreadmodel interface , the servlet container need to reuse the same thread for multiple requests to same servlet.
No. If you use STM you're asking the container not to have two request threads use the same servlet instance at the same time, not to use only a single thread.
But it is creating a new thread for every request.
That's indeed how it should work.
It is using same thread(not creating new) and same instance when used in firefox and is now just working as i expected.
Anyhow thank you for your support.
When i open the same servlet in three different browser windows and run them one after another quickly,i expect that each counter is unique,but i am getting same counter for all..
The counter variable is local to the service method which explains the behavior you are noticing.
In order to get unique counter values, you just have to declare the counter as an instance variable of SingleThreadedServlet2.
...the servlet container need to reuse the same thread for multiple requests to same servlet.But it is creating a new thread for every request.
The mechanism of request dispatch to a service thread by the Container is completely container implementation specific.
There's no way for the servlet developer to instruct the container to do that.
The container "may" end up reusing the same thread,assuming it's using a Thread Pool,to service different requests.But then the servlet developer has no control on that.
[ August 29, 2008: Message edited by: Ajay Saxena ]
I have taken the code from this book
Please go through 5 pages from page 37.
Here to prevent simultaneous access to a common external resource,(i.e in order to prevent any uncommitted reads) we are trying to use a SingleThreadModel.
Well , i am not bothered about whether the container is using the same thread or not..but i am bothered why it is allowing multiple threads to access same resourse even though when i mentioned that it has to implement SingleThreadModel..
one more thing to add..My IE7 is behaving the same unexpected way even if i make the counter varaible as instance variable...
please try to run the servlet and analyse the output in console...
hope you got my question..
why it is allowing multiple threads to access same resourse even though when i mentioned that it has to implement SingleThreadModel..
You're misunderstanding what STM is supposed to do. Nowhere does it say that multiple threads can't run the same servlet instance. In fact, that is to be expected. The only guarantee that's given is that multiple threads won't do so at the same time.
[ August 29, 2008: Message edited by: Ulf Dittmer ]
The only guarantee that's given is that multiple threads won't do so at the same time.
but i was also asking the same...but forgot add 'at the same time' in my previuus post...so..now my question become..
why it is allowing multiple threads to access same resourse at the same time even though when i mentioned that it has to implement SingleThreadModel.. ..
If possible some one run this servlet in Internet explorer only(preferably 7)..and observe the result..It is allowing to access same resourse for more than one thread at the same time
It is allowing to access same resourse for more than one thread at the same time
How do you know that? What exactly are you doing that leads you to conclude this? (People are unlikely to take the time to run the code themselves, so we rely on you telling us the details of what's happening.)
so as per my code..when first request hits the server..it reads the file from data and increments it but before it could update the incremented value into the new file it is forced to sleep for 6 seconds..during these 6 seconds the second request hits the server...as per STM..the server should not allow the second thread to read the uncommitted value(i.e say when the first request is hit say the count is 5..then it should be 6 for second thread....but it is still 5 as the first thread is sleeping) from the file till the first thread completes it's execution..
so i am getting same value 5 in all outputs..and in console i can see that the thread ids are not unique...
so i concluded that multiple threads are created and simultaneous access was allowed...actually i should get outputs as 6 , 7 and 8..but getting 6 6 and 6...
hope i made it clear..