• 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

Thread mechanism for Request in web container

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does threads work in web container ?

When a web container receives a request for a servlet, it creates thread for this request. Is this java thread or OS thread ?

This is what I read from a book Head First Servlets and JSP - page No 41, print publication date - 8/1/2004

"The container automatically creates new java thread for every servlet request it receives"

That means the threrad is java threrad. Which means the object is passed as an argument when thread is created. This object has to implement runnable nterface."

Is the object being passed as an argumrent is the same servlet which has been initialized ? If yes, then servlet does not

implement runnable interface. It does not have run() method. How can it be passed as argument ?

The object being passed is HttpServletRequest. This will implement run() method and code within this method calls the methods.

While we need servlet's doGet() method or doPost() method to run and we pass HttpSDervletRequest ans HttpServletResponse objects to these methods. So of what object do we create the thread ? How does servlet call objects from these methods in to doGet() method and doPost() method ?

In the main() method of container servlet is initialized, then we instantiate threads in this main() method. Can thread class call the servlet without instantiating a servlet in it ?


how to call servlet doGet() method or doPost() method by the thread in here ? or how to pass HttpServletRequest object to doGet() method of servlret ? is entire thread passed as an object ? How does this mechanism work ?

Does HttpServletRequest implement runnable interface ? If not, then how can a java thread be created of it ? As it needs

to implement runnable interface and have run() method.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes you think that the request needs to be the runnable component? That would, in fact, be very very odd.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, The threads are of 2 types - green threads and OS threads, green threads are java threads which you mentioned which is maintained by the JVM itself unlike the OS threads maintained by the OS.
But most of the JVM implementations leaves the thread scheduling and maintance to the OS! so though you create a java thread by initializing a new Thread object, it is eventually an OS maintained thread.

Secondly, all webcontainers would create a pool of threads and make it wait for incoming requests, once a request arrives, a thread in the thread pool takes responsibility of serving that particular request by interpreting the request to a proper servlet method call. once the response is flushed to the client, the thread goes back to the pool.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic