This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet Thread Model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Thread Model " Watch "Servlet Thread Model " New topic
Author

Servlet Thread Model

maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
All,
I have a question that folks here at JavaRanch may be familiar with.

Could someone please explain servlet instantiations (probably in Tomcat) using a Single Threaded Model versus Multi-Threaded Model?
My confusion basically refers to this:
If I have servletA requested at the same time by two different requests, what happens? Does a new instance of the servlet get initiated for each request? If not, how is it synchronized?
Thank you.
cyberCipher


Sun Certified Java Programmer 2 (1.4)<br />Sun Certified Web Component Developer
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"cyberCipher"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks! and welcome to the JavaRanch!
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ghulam Rasool
Greenhorn

Joined: Aug 30, 2003
Posts: 4
servletA will not be instantiated for each request. Basically each request is treated as a thread accessing the same instance of the servlet. And the Servlet is NOT synchronised. u have to IMPLEMENT Synchronization for the data u want to be threadsafe.
But the scenario is different when servletA implements the SingleThreadModel interface(i.e. a new instance is created for each request)
thats it ....

..GR..
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
Thanks for your reply.
since servletA will not be instantiated for each request, is it possible for two requests to get each other's information.
e.g. servletA accesses a bank account balance through a stateless ejb. Is it possible for userA to get userB's account info and vice versa, if they access the servlet at the same time? If this is the case, is the doPost method not synchronized?
Thank you,
Maneesh
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
You have to have a major shift in your programming viewpoint and habits to use servlets.
1. HTTP is inherently state-free - it is up to your program to track the state of each user and remember it between requests. Typically this is accomplished with the HttpSession class. The servlet engine provides methods to associate a "session" with a particular user.
2. You can't use instance variables in the servlet for ANYTHING that is unique to a given user - you must use "local" variables. Any variable state that must be saved between requests can go in the session.
You should find a good servlet tutorial to get a clear picture of whats going on in a servlet/JSP web server.
Bill
maneesh subherwal
Ranch Hand

Joined: Aug 26, 2002
Posts: 42
I agree with you that the variables that are used must be local. The bankaccount is a local variable in my example described above. I assumed that everyone would understand that but I was slightly unclear.
Now, considering this is the scenario, how will the example given react in both cases?
e.g. servletA accesses a bank account balance (local variable) through a stateless ejb. Is it possible for userA to get userB's account info and vice versa, if they access the servlet at the same time? If this is the case, is the doPost method not synchronized?
Since the doPost() method is called with the request, response sent as parameters, is it impossible that the user's accounts will be interchanged?
Thank you,
Maneesh
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
If I understand your problem correctly, if there is only one instance of the EJB being used by all requests it is up to the EJB to distinquish between the two users in a Thread-safe fashion.
It makes more sense to synchronize on the EJB instance than to synchronize the entire doPost metohd. The reason being that generating the text of the response after the (local variable) balance has been located may be time consuming but will not interfere with other requests.
Each request and response are objects unique to the Thread handling the request - they are created and managed by the servlet engine.
Bill
 
jQuery in Action, 2nd edition
 
subject: Servlet Thread Model
 
Similar Threads
servlet concepts
Servlet Container
Servlet Thread Model
when to use SingleThreadModle
Multithreading in the new Beta