• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Each request runs in a separate thread?

 
Jelo Nehuptra
Ranch Hand
Posts: 36
Java jQuery Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain how is this done? I mean, HttpServlet nor HttpServletRequest doesn't implement Runnable nor extend Thread?
 
Paul Clapham
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic