This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about instance of servlet

 
david hu
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic