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.
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
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).
[ SCJP6, SCWCD5 ]
Curse your sudden but inevitable betrayal! And this tiny ad too!
Devious Experiments for a Truly Passive Greenhouse!