• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Each request runs in a separate thread?

 
Ranch Hand
Posts: 36
jQuery Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain how is this done? I mean, HttpServlet nor HttpServletRequest doesn't implement Runnable nor extend Thread?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic