• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in servlet basics

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
bye.
Loke.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lokesh,
Great!!That was very nicely explained.
Regards-----Pradeep
reply
    Bookmark Topic Watch Topic
  • New Topic