This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doPost runs in its own thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doPost runs in its own thread?" Watch "doPost runs in its own thread?" New topic
Author

doPost runs in its own thread?

Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
What's true about the lifecycle of a servlet? (Choose all that apply.)
A. The service() method is the first method invoked by the container when a new request is received.
B. The service() method is invoked by either doPost() or doGet() after they have completed a request.
C. Each time that doPost() is invoked, it runs in its own thread.
D. The destroy() method is invoked after every invocation of doGet() completes.
E. The container issues a separate thread for each client request.

HFSJ 2nd edition question 43 answer

Now the option C has been marked correct by the book, I have seen this is previous posts but they did not clear my doubt about Option C.

From what I know when a new request comes in container creates a new thread and calls service, the service figures out that we have to call doPost/doGet etc. and calls it by that thread only.

So the request thread is shared by doPost and service methods and doPost thus does not have an exclusive thread.
So option C must be wrong.


Parth Tiwari
| Pursuing Bachelor of Engineering | OSUM Club Leader | SCJP 6 | SCWCD 5 |...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

From what I know when a new request comes in container creates a new thread and calls service, the service figures out that we have to call doPost/doGet etc. and calls it by that thread only.

So, doPost runs in its own thread, doesn't it ? The servlet is shared, but the thread is different, one per request.


[My Blog]
All roads lead to JavaRanch
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
but the thread is shared by two methods na..
service() and doPost()
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I see what you mean The wording is confusing in the answer. I think they want to check that you know that each request will be served by a different thread, so doPost, doGet... will be served by a different thread. In the answer, i think that "it runs in its own thread" doesn't mean that doPost starts a new thread, but that the container started a new thread to service the request.
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
ok thanks..
i feel the answer is ambiguous
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks for sharing, What about the option A? Could you guys elaborate? I think, A is also an answer!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1661
    
  25

Hi Abimaran,

The first request goes through the service method, but only after the init(ServletConfig sc) is executed.

Regards,
Frits
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Frits Walraven wrote:
The first request goes through the service method, but only after the init(ServletConfig sc) is executed.
Frits

Thanks for reply, Yea, I know it, but I need a clear idea about this question. That's why I asked. What does mean by new request? the first request to the corresponding servlet? or just another new request to the servlet? It's confusing to me! Thanks.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Parth, I agree that the answer is a little ambiguous, but you don't have to worry about the real exam. There are no ambiguities in the real exam.

Abimaran, A does seem to be the correct answer at first, but before the service method is called, the container does stuff like prepares the request and response objects, prepares the session if it exists etc. So the service method is not exactly the first method the container calls. The container has to do some preparation and call few methods before the service method is called...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:
Abimaran, A does seem to be the correct answer at first, but before the service method is called, the container does stuff like prepares the request and response objects, prepares the session if it exists etc. So the service method is not exactly the first method the container calls. The container has to do some preparation and call few methods before the service method is called...


Thanks Ankit.

Is this apply for servelts which are already constructed, initialized and used to serve the request from some other clients and even the same client? I think, it's depends on servlet's purpose, if the servlet wants to know the session of the user, adn other stuff, then service() method will be called after all these? Correct? Please confirm.

Thanks!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Ever service method receives a request and response, if nothing else they'll have to be initialized by the container...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doPost runs in its own thread?