• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ 2nd edition question 43 answer seems ambiguous

 
Jean Robillard
Greenhorn
Posts: 17
  • 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.

The answers are C and E. However, I think that A and C are highly debatable.

For A: the wording of "when a new request is received" is vague. I think the author meant "when the very first request is receved by the servlet". Then yes, I agree that service() is not the first method called, but rather init(). But the current wording to too vague to learn toward one way or the other, because we don't know if it's the very first request or not. I believe all requests are "new" requests!

C is debatable too. Each time service() is invoked, it runs in its own thread. So it's rather obvious that doGet() does also. So while C is not technically wrong, the problem is that it's equally likely for another test author to say "C is wrong, it's service() that runs in its own thread" and our choice would have been denied.

Any comments... ?
[ September 09, 2008: Message edited by: Jean Robillard ]
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jean Robillard:


C is debatable too. Each time service() is invoked, it runs in its own thread. So it's rather obvious that doGet() does also. So while C is not technically wrong, the problem is that it's equally likely for another test author to say "C is wrong, it's service() that runs in its own thread" and our choice would have been denied.
Any comments... ?

[ September 09, 2008: Message edited by: Jean Robillard ]


Here are my two cents about C :
1. If we read and try to answer the question then I would have also selected C as one of the correct answer.
2. But because you raised the question gave some more thoughts and then I think this may not be correct because
2.1 doPost() can be called from doGet(). In many instances it is done like that. In such cases doPost() will not run in a new thread. Rather the thread which was created to serve the HTTPRequest will be the one.
2.2 Also as per Servlet specs service() method is called to serve the HTTPRequest and then the control goes to either doGet or doPost or other doXX Methods. In this case also doPost() will run in the context of the thread started by service method.
 
Eurig Jones
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question really annoyed me too...

A. Well the service() method IS the first method invoked by the container when a new request is received. The answer states that it's init() that gets invoked first!? Well init() isn't invoked when a request comes in!

C. It implies that it doPost specificaly has it's OWN seperate thread. This is not true.
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My 2 cents:

cent 1, about A:
Originally posted by the API:
public void init(ServletConfig config) throws ServletException

Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.

So init is called before a new request is received.

cent 2, about C and E: when the servlet implenents SingleThreadModel, the container might queue all the requests, and run them in one thread.
[ September 10, 2008: Message edited by: Jan Sterk ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic