• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doPost runs in its own thread?

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but the thread is shared by two methods na..
service() and doPost()
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks..
i feel the answer is ambiguous
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing, What about the option A? Could you guys elaborate? I think, A is also an answer!
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic