File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Clear my doubts please ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Clear my doubts please ..." Watch "Clear my doubts please ..." New topic
Author

Clear my doubts please ...

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

I have this small doubt about Servlets :

When it's said that there's a seperate thread for each request, is that seperate thread the doGet or doPost method ???

Please clear my doubt and advice me ...

Best regards ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
sweety singh
Greenhorn

Joined: Dec 04, 2006
Posts: 13
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
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Nice nice

But what about the doGet and doPost ?
Are they seperate threads ?

Thanks alot ...
sweety singh
Greenhorn

Joined: Dec 04, 2006
Posts: 13
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
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

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 ...

Please i need a clear explaination ...

Thanks in advance ...
Saurabh Kumar
Ranch Hand

Joined: Aug 21, 2006
Posts: 56
Yes, doGet/ doPost run in seperate thread.

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.

Hope this helps...

Thanks and regards,
Saurabh
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Thank you Mr.Saurabh ...

Best regards ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clear my doubts please ...