• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Mock Exam 43  RSS feed

 
Ranch Hand
Posts: 1684
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What is true about the lifecycle of a servlet?
Option A. The service() method is the first method invoked by the container when a new request is received.
....


I think this option is correct, but the given answer says it is not correct.
Explanation:


Option A : the init() method is invoked first.


My reason:
I assumed the servlet has been instantiated and initialized(), when a new request is received,
the container calls service() method which invokes the doGet, doPost or other do methods depending on the request's action.
 
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 3098
255
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I assumed the servlet has been instantiated and initialized(), when a new request is received


No, that is not always the case:

Form the Servlet specs:

The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request.

and

After the servlet object is instantiated, the container must initialize the servlet before it can handle requests from clients. Initialization is provided so that a servlet can read persistent configuration data, initialize costly resources (such as JDBC™ API based connections), and perform other one-time activities. The container initializes the servlet instance by calling the init method of the Servlet interface with a unique (per servlet declaration) object implementing the ServletConfig interface.

 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!