Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Correlation between Servlet's init() and service() methods

 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,

One of my friends got this question, as a part of his interview questions. We have been thinking about this, from then on. We would like to have a better understanding. That brings us here...well, the question is...


Can service() method be called from init() method? If so, what's the use of having a separate service method.


We know that it's a lifecycle method and init() will pass the Request,Response objects to the service method. Can you please care to expand a bit on these?

Your help is much appreciated.

Cheers,
Ram.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the init method (as it's name implies) gets run when the servlet is loaded. The service method on the other hand, waits for requests and responds to them.
Look at the argument list for service. It requires a request and response object.
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ramaswamy ,
init() is called only once & not for each request & also if you try to call service from init how you will get Request & Response Object. Somehow suppose you get them then as said earlier,init will be called only once.


I hope this helps.......

By the way if you don't mind what was your friends answer & where he got that job?
Shrinivas
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody contemplating working seriously with servlets and JSP should have a copy of the API on hand to refer to. It really is very clear on such things as the role of init and service - why ask questions on this forum when you could refer to the source?

Download links to the specifications can be found here for the 2.2, 2.3 and 2.4 APIs.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic