Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling init() method from service method in servlet

Tags:

java

servlets

Can we call init() method from service() method in servlet ? I got this as an interview question. Why would anyone do this anyway ?

like image 274
user2434 Avatar asked Jun 26 '26 23:06

user2434


1 Answers

No reason to call init from service, init is meant to be called by the container - to initialise it with configuration (as the name suggests) and to allow it to do any expensive operations it needs to do (setting up connections to database or whatever).

It may make some sense if you wanted to programmatically reconfigure your servlet to call the init from the servlet itself, but I struggle to see this use case.

like image 116
maksimov Avatar answered Jun 29 '26 14:06

maksimov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!