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

Head First Mock Exam 43

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • 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.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic