File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Doubt in servlet basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Doubt in servlet basics" Watch "Doubt in servlet basics" New topic

Doubt in servlet basics

kiran kumar73

Joined: Nov 13, 2000
Posts: 6
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

Joined: Sep 15, 2000
Posts: 66
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

Joined: Nov 09, 2000
Posts: 10
Hi Lokesh,
Great!!That was very nicely explained.
I agree. Here's the link:
subject: Doubt in servlet basics
It's not a secret anymore!