Thread A : The Container calls init() on the servlet instance after the servlet instance is created but before the servlet can service any client requests. If you have initialization code (like getting a database connection or registering yourself with other objects), then you'll override the init() method in your servlet class. Otherwise, the init() method from GenericServlet runs.
Thread B: When the first client request comes in, the Container starts (or finds) a thread and causes the servlet's service() method to be invoked. You normally will NOT override the service() method, so the one from HttpServlet will run. The service() method figures out which HTTP method (GET, POST, etc.) is in the request, and invokes the matching doGetQ or
doPostQ method. The doGet() and doPost() inside HttpServlet don't do anything, so you have to override one or both.
Thread C: When the second (and all other) client requests come in, the Container again creates or finds a another thread and causes the servlet's service() method to be invoked.
So, the serviceQ -> doGet() method sequence happens each time there's a client request. At any given time, you'll have at least as many runnable threads as there are client requests,
limited by the resources or policies/configuration of the Container.
SO CAN WE INTERPRET THAT FOR EACH SERVLET A THREAD IS CREATED HAVING ITS OWN INIT() BEING CALLED BEFORE REQUESTS COMES IN & AFTER THE REQUESTS COME IN ANOTHER SECOND THREAD IS CREATED FOR THAT SERVLET???
Thanks in advance..
P.S= this book is awesome , soon will be appearing for scwcd.
You are right! the most important point here is that Servlet.init(ServletConfig config) will be called ONLY ONCE in the life cycle of a servlet. Also there will be only one instance of a servlet at a time (provided the app is not a distributable one). For each request coming from client, the container will create separate Threads, create objects for HttpServletRequest and HttpServletResponse and pass to the service method which will be run in that new Thread. The service() method will decide which doXxx() method to call!. For example, if there are 10 requests coming for a servlet, container will create 10 separate Threads and run the service() method in 10 separate Threads. But there will be only a single instance of the servlet class.
I do believe this cannot happen but certain clarification is required since the book seems to support the above scenario.
Thanks for the response ,looking forward for the same.
Lets wait for more opinions!
Good luck with exam!
when the request comes in, hten1st thread is created for the servlet calling its init() further a second thread is created for the service() method OR is it that initially all the servlet have their individual threads created having called their individual init() called further as request comes in further threads are created ( doubtfull of this one happening because all the servlets would be initialized which would crash down the server ).
Firstly, the calling of the init method is not strongly correlated with requests coming in; did you understand that part in my previous post?
Secondly, only a single servlet instance is created -no matter how many requests are coming in- so there's only a single time that init is called.
Lastly, initializing even a hundred servlets should not do anything bad to a server, except delay the responses. Of course, if the servlets take a long time to initialize, then they should be declared as load-on-startup, or -even better- whatever is initialized should be moved to a context listener.