File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet requesting threads

 
Bob Moranski
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a general question on how a servlet is invoked by multiple requests. Are these requests each a thread that all have access to the same <i>instance</i> of the servlet? If that's the case, can we consider a servlet also a implicit Runnable or Thread?
Thanks!
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Servlet isn't a Runnable or a Thread, it's just a class used by one. You wouldn't consider String to be implicitly a Thread, just because you use one in a thread, would you?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is critical to note is that all instance variables of servlets are shared by all users of the servlets.
 
Bob Moranski
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, my point is that all requests access the same instance of the servlet?
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessarily.
In most servlet containers they probably do use the same instance, (unless the serlvet is marked as SingleThreadMode), but the spec makes no guarantee about that, and allows container authors to use any strategy they feel is appropriate - single instance, multiple instance in the same JVM, multiple instances in different JVMs, multiple instances on separate machines and so on ...
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly! A well designed server may, for example, create multiple instances of a servlet to handle server load.
 
Bob Moranski
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys Frank and Thomas!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic