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 Each request runs in a separate thread? 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 "Each request runs in a separate thread?" Watch "Each request runs in a separate thread?" New topic
Author

Each request runs in a separate thread?

Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

Can anyone explain how is this done? I mean, HttpServlet nor HttpServletRequest doesn't implement Runnable nor extend Thread?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Something in the thread creates one of those objects, or more likely is given one of them by the container, and then calls its service method. Of course this would be done as part of the something's run() method.
Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

so with each client request, a servlet (servlet per request) handles the client request, meaning one serlvet per request? it isn't a servlet singleton?

i just remembered in c# web services that you can make the server a singleton, reentrant, or per request via the "annotations", can you do the same thing using the Java EE 5?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It isn't a singleton in the sense that it's guaranteed there will only be one instance of each servlet in the container. On the other hand the container will probably only create one instance of each servlet, unless it's forced to create more. So no, it isn't one instance per thread.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Container 's Thread instances representing the request processing thread may contain an instance variable "HttpServletRequest"... When request is received, container may create an instance of HttpServletRequest and assign this instance to one of request processing thread from the Thread Pool...
Likewise, for each request processing thread instance, HttpServletRequest instance is assigned to get request processed.


SCJP 6 [SCJP - Old is Gold]
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

I had similar doubt. Instead of posting a new topic, I am asking here.

Well, I didn't completely understand this sentence: "The Container finds the correct servlet based on the URL in the request, creates or allocates a thread for that request, and calls the servlet’s service() method, passing the request and response objects as arguments."

Allocation of a thread and calling servlet's service() method: is service() method called inside the allocated thread's run() method? That means, the thread will call the service() method right? If thread doesn't call service() method, then what is the use of allocating a thread?
Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
Hello faisal,
"The Container finds the correct servlet based on the URL in the request, creates or allocates a thread for that request, and calls the servlet’s service() method, passing the request and response objects as arguments."
.
Above lines explains the flow of request and response for a web app.Check your Deployment Descriptor(web.xml).Your app's web.xml is the access point for the request to reach the correct servlet it is ment for.
Once container gets a request ,it maps the URL in in web.xml as servlet mapping and assign a thread to the request .This thread accesses service method of the matching servlet which takes request and response as the arguments.
Thats one of the many roles of the container.

If the servlet class file gets loaded thread will call the service method other wise container will not able to find the correct servlet and will throw ClassNotFound.And assigned thread is ment for calling the service method which depending upon the type of request calls doGet or doPost .


Thanks
Shashank
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Each request runs in a separate thread?