hi! when the web application is loaded, container creates instance of servlet,just one instance of servlet created, whenever a new request comes container creates a new thread and same objeat is passed to the new thread eg Myservlet ser=new Myservlet();(suppose this is the object created by container ) Thread t1=new Thread(ser);-----for one client
Thread t2=new Thread(ser);-----for another client passing same object means both thread will do the same job but for differnt clients as and when they get request from the client
Joined: Mar 08, 2007
But what about the doGet and doPost ? Are they seperate threads ?
hi when first thread t1 is in execution it will call ser(servlet) doGet()or doPost() and when thread t2 is executing it will again call ser doGet() or doPost() there is nothing like methods are running in threads
Joined: Mar 08, 2007
I meant, does it (doGet or doPost) run in a seperate thread (thread of execution) ?
There's this thing that I'm not getting : the container creates the thread, and then it calls the services method passing it the request and response objects, BUT, what is the purpose the thread ???
It's really annoying me
What does that thread do ?
And the service method itself calls the doGet or doPost ...
service/doGet/doPost, being instance methods need to be called on the Servlet instance. But the container doesn't create seperate instances for each request(rather request thread).Instead it uses the same instance to invoke service(and hence, doGet/doPost). Executing each request in seperate thread enables container to handle multiple request simultaneously without creating numerous servlet instances which may take all container resources.
I am not sure of how container manages request threads but here is what happens w.r.t doGet/doPost.
First Request: Container receives request, creates an instance of this servlet, calls service() on this instance, service() delegates to doGet/doPost.
subsequent Requests: Container receives request, uses the instance already created(No new instance is created), calls service(), service() delegates to doGet/doPost.