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

Servlet

 
dharmendra Rathor
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements are true?
Choose at least one answer.
a. The init method is called each time a Servlet service method runs
b. The init method is called by the container when it is placed into service
c. The init method cannot be overriden because it is marked as final
d. When a container shuts down it will call the undeploy method of running servlets
e. When a container shuts down it will call the destroy method of running servlets

Answer e ,b
Is b correct answer ?
 
Durga Prasad Vuyyuru
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rathor,

The container will call the init() after the servlet class loaded.And it will be called only once in the servlet's life cycle.
The use of init() is to intialize basic requirements like getting a database connection etc(init() will ensure that all the things that are required by the servlet is avaliable to it,when a client makes a request).

And comming to the destroy(),this too will be executed once in the life cycle.it is to relase the resources held by the servlet,when the application is undeployed or when the server is shut down.

I hope you got the thing.
 
Sravan Kumar Kollapudi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer B is wrong. The container need to call init method even before service method is kicked off.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container need to call init method even before service method is kicked off.



Placing a servlet into service and calling the service method are 2 different things

The service method is called when you make a request(POST, GET, etc) to a servlet.

You can see the difference if you place a display in the init method of a servlet and tell the container to load it on startup (setting in your web.xml file). The display will execute even before you send a request to the servlet
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic