File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doPost runs in its own thread?

 
Parth Twari
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Parth Twari
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the thread is shared by two methods na..
service() and doPost()
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks..
i feel the answer is ambiguous
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing, What about the option A? Could you guys elaborate? I think, A is also an answer!
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2164
57
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ever service method receives a request and response, if nothing else they'll have to be initialized by the container...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic