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.
hi! i got a doubt in basics of servlet.pls help me. there is a html form from which a servlet is called.when i click the submit button a thread will be created at the server and it will be executed and the result will be sent back to the client.but if i click the submit button 500 times simultaneously, will it create 500 threads or only one thread.what is the reason. pls help me out in this. thanking you in advance.
Hi Kiran, First, let me say about the servlet life cycle. The servlet life cycle consists of three stages. 1) init. 2) service. 3) destroy. The servlet invocation happens like this. 1) Client Send request to webserver. 2) Webserver, after identifying it as a servlet, sends it to a servlet container. 3) The servlet container checks whether the servlet is already in jvm or not. If the servlet is not loaded, the servlet container loads the servlet using the custom class loader. 4) Once the servlet is loaded, it becomes persistant. i.e it stays quite for some time, i.e it is ready to serve as soon as the request is made. This is the reason, why there is a slight delay in invoking a servlet for the first time. The init method gets invoked only once, that is the first time the servlet gets invoked. The service method gets invoked for each request made by client. The destroy method gets invoked when the servlet is getting unloaded from the jvm. So, if u have initialied a thread in the service method, the number of times you make a request, the same number of threads will get created. So instead of initializing a thread in the service method, do the same in the init method. This ensures you that, only one thread will get created and it serves all the request. And dont forgot to stop or destroy it in destroy method. I hope you understood the concept. If u still have any doubts, then i would like to suggest you to contact my guru, Mr. Rahul Mahindrakar, who is a bar trender of javaranch and he is dealing with threads concept. I hope u find this info useful. bye. Loke.