Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ 2 ed. Final mock exam 43 question

 
Kiril Nugmanov
Ranch Hand
Posts: 42
  • 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’ve 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.


In the book C and E are correct but I thick that A also can be correct. If for example would be used <load-on-startup>1</load-on-startup> element in DD. Then init() method would be called by container but not when the request is coming. As I understand init() would be called first if there would be first request of the servlet and there is no any additional pre-compile and loading on startup options.

Am I correct?
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kiril.
A. The service() method is the first method invoked by the container when a new request is received.

I think, it is wrong. If we don't use <load-on-startup> service() method is not the first method invoked by the container. If we use <load-on-startup> it is first method.
 
Alex Bluebird
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure.... but it may have to do with the word "New" ....it says a "when a New request comes in"......it may mean the first request that invokes the servlet for the first time, when the servlet is "new", init() is invoked..... but obviously with each subsequent request it should be service()
Some times the way they formulate the questions is quite confusing
I ticked C & E too
 
sandeep kapse
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think 'E' is the correct one.
Because service() method is not the first method invocked, the flow is class-load->constructor->init(). After this servlet is initialized then on each request separate thread is created and service method is called in that thread. This is my understanding on what i read till not in HFSJ.
And 'C' is also the choice in the sense that service() method internally calls doPost()/doGet(), but in case of HttpServlet. In the case of GenericServlet (not specific to http) I found 'E' the correct one.
(please correct me if i am wrong).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic