This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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.
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
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?