This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in servlet basics  RSS feed

kiran kumar73
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
lokesh reddy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
pradeep kurup
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lokesh,
Great!!That was very nicely explained.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!