I've a servlet that made use of its init method to read a config file and set the values into the servletcontext. Whenever a new HTTP request comes in, will the container creates just 1 instance of the servlets, or many instances could be created?
If 1..* instances of the servlet could be created, does it means that the init method will be called each time an instance is created?
Originally posted by Ulf Dittmer: Only one instance is created. I'm surprised that this isn't covered by the SCWCD exam, which -judging by your signature- you have taken.
In the case where a servlet was deployed as part of an application marked in the deployment descriptor as distributable, a container may have only one instance per servlet declaration per virtual machine (VM).
I was reading the specifications and found the use of the word may, hence the query. Wouldn't this means its up to the servlet container?
Chengwei Lee
Ranch Hand
Joined: Apr 02, 2004
Posts: 884
posted
0
Originally posted by Amol Nayak: Is your servlet implementing SingleThreadModel?
Originally posted by Amit Kumargupta: If you implement SingleThreadModel interface the server will create multiple instances of servlet.
This is not true. Firstly the question is about Servlet instances and the init() method, not SingleThreadModel.
But since it was brought up, this is from the API:
If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.
(bold added by Dave) Neither of these mention a new instance for each request, although it is a less efficient version of the second option. [ August 03, 2007: Message edited by: David O'Meara ]
Chengwei Lee
Ranch Hand
Joined: Apr 02, 2004
Posts: 884
posted
0
Originally posted by David O'Meara:
(bold added by Dave) Neither of these mention a new instance for each request, although it is a less efficient version of the second option.
[ August 03, 2007: Message edited by: David O'Meara ]
Would it then be safe to assume that it's 1 instance per servlet per JVM, or it's vendor-specific? You're right that it's not mentioned that each request will get a new instance, but I can't find seems to locate where it states that it is guaranteed to be exactly 1 instance per servlet per JVM.
It is one Servlet instance per servlet mapping, since a Servlet class is able to be mapped several times in web.xml
Chengwei Lee
Ranch Hand
Joined: Apr 02, 2004
Posts: 884
posted
0
Originally posted by David O'Meara: It is one Servlet instance per servlet mapping, since a Servlet class is able to be mapped several times in web.xml
Originally posted by Chengwei Lee: I've a servlet that made use of its init method to read a config file and set the values into the servletcontext. Whenever a new HTTP request comes in, will the container creates just 1 instance of the servlets, or many instances could be created?
If 1..* instances of the servlet could be created, does it means that the init method will be called each time an instance is created?
Hello Mr.Lee,,
First thing to note is ,only one instance of servlet will serve all the request's ,secondly multiple instances are created of a servlet when you implement single thread model,Third thing is init method is called only once for every instance of a servlet,
If you are implementing SingleThreadModel, then obviously, for superior performance you'd want the servlet container to create multiple instances of a SingleThreadModel type servlet should there be many requests received in quick succession. Whether or not a servlet container does that depends completely on the implementation. So it is not guaranteed that if you implement SingleThreadModel, the container will create multiple instances for that servlet.
Originally posted by Prem Kashyap: If you are implementing SingleThreadModel, then obviously, for superior performance you'd want the servlet container to create multiple instances of a SingleThreadModel type servlet should there be many requests received in quick succession. Whether or not a servlet container does that depends completely on the implementation. So it is not guaranteed that if you implement SingleThreadModel, the container will create multiple instances for that servlet.
Regards Prem Kashyap SCJP 1.4 : 88%
Prem, We appreciate your participation in these forums. Before doing so, however, please take the time to read the entire thread. In this case the original poster has already stated that he is not using SingleThreadModel. Adding any discussion of SingleThreadModel after that only makes the thread longer and more confusing.
First thing to note is ,only one instance of servlet will serve all the request's ,secondly multiple instances are created of a servlet when you implement single thread model,Third thing is init method is called only once for every instance of a servlet,
Santhosh Reddy, We appreciate your taking the time to answer questions in this forum. Before doing so, in the future, please read the entire thread. In this case the original poster's question has already been answered. Answering again, especially if you're not elaborating on the first answer, only makes the thread longer and more confusing.
I was reading the specifications and found the use of the word may, hence the query. Wouldn't this means its up to the servlet container?
The init method is called when the servlet is put into service. The servlet spec allows containers to take a servlet out of service if it wants to (with the idea that a container might want to take a rarely used servlet out of service to conserve resources). In this case, the init method would be called again when and if the servlet were put back into service.
This is one of the reasons that we push the use of context listeners for application initialization code over the older method of putting this code in the init method of a servlet and then using load-on-startup to push this code to be run when the app starts.
Prem Kashyap
Ranch Hand
Joined: Oct 10, 2006
Posts: 52
posted
0
================================================= Prem, We appreciate your participation in these forums. Before doing so, however, please take the time to read the entire thread. In this case the original poster has already stated that he is not using SingleThreadModel. Adding any discussion of SingleThreadModel after that only makes the thread longer and more confusing.