File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Single Thread Model question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Single Thread Model question" Watch "Single Thread Model question" New topic

Single Thread Model question

rajakumar babu

Joined: Nov 12, 2003
Posts: 9
In the below jsp, the page directive attribute isThreadSafe is "false" => implements SingleThreadModel. So, when I opened this page in 2 different browsers, the result should be the same (ie. 0,1,2,3,4,5,6,7,8,9) since container should create 2 instances of the same jsp. But, the output i am getting is (0,1,2,3,4,5,6,7,8,9 - in Browser1) and (10,11,12,13,14,15,16,17,18,19 - in Browser2)
Why is it behaving this way ?
<%@ page isThreadSafe="false" %>
<%! int j=0; %>
for (int i=0; i<10; i++)
out.print("The value of j is " + j + "<br>");
Dominic Paquette
Ranch Hand

Joined: Dec 13, 2002
Posts: 64
I think the IsThreadSafe = false means that your generated servlet class will NOT implement the SingleThreadedModel.
Shiva Mantri

Joined: Dec 30, 2002
Posts: 19
if isThreadSafe=false, then the servlet container implements SingleThreadModel. What this means is that only one thread(or request) will be able to execute service method of the servlet instance. Depending on the load on the container, the container may choose to initiate multiple intances.
Whats happening in your code is, the container created only one instance and both the requests were handled by the same servlet instance. One request incremented the count of member variablet to 9, and the next request, which was handled by the same instance, so you get from 10-19.
bottomline: SingleThreadedModel doesn't guarantee that you will get a new instance for every request (xref: ServletSpec2.3 - SRV.2.2 or SingleThreadModel interface)
[ November 13, 2003: Message edited by: Shiva Mantri ]
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
So in your case, all happens as if your service() method was synchronized, that means:
- it prevents the container from calling the service() method of your servlet from multiple threads.
- just one instance of your servlet is created.

SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
rajakumar babu

Joined: Nov 12, 2003
Posts: 9
Thanks, it helped me a great in understanding the STM Interface,
I agree. Here's the link:
subject: Single Thread Model question
It's not a secret anymore!