• 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

Servlet Thread Model

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic