• 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

about instance of servlet

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Suppose I use one browser window to connect to the servlet and use another browser window or another machine to connect to the same servlet at the same time(both connection may continue for long time), will the server create two instance of the servlet to response the request or just one instance? Another question is will the server create two threads to respond the two request?
I am a little confusing about it. Wish someone can give me some help
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes it depends on the servlet engine or application server you are using, but in most cases, there exists only 1 instance of the servlet, and this single instance services all requests. This is why it's important to make your servlets thread-safe. And yes, if you access the servlet from 2 different contexts, or even the same context (same machine), at the same time, then the server will use two different threads to access the servlet. And again, it is for this reason that you must ensure your servlet is thread-safe.
SAF
 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not quite understand from where or how the servlet spawn a new thread. Since you know that in the servlet I wrote, there's no statement in it that explicitly spawn a new thread to respond a new request.
How does it work?
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading the servlet spec will clear up a lot of your questions.

Specifically, I've copied some of it here

SECTION SRV 2.2 So that describes how many instances of your servlet are hanging around. In most cases, just the one. So concurrent requests must be handled by one servlet, and this is were multiple threads comes in.

From SECTION SRV.2.3.3.1That was the long answer. The short answer is: The container is doing the threading, not you. You simply need to write your servlet so that it can handle these multiple threads.
 
reply
    Bookmark Topic Watch Topic
  • New Topic