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
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 ....
Joined: Aug 26, 2002
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
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
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
Author and all-around good cowpoke
Joined: Mar 22, 2000
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