This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Servlet instances and its init method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet instances and its init method" Watch "Servlet instances and its init method" New topic
Author

Servlet instances and its init method

Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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?


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
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.


Ping & DNS - my free Android networking tools app
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Is your servlet implementing SingleThreadModel?
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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
Originally posted by Amol Nayak:
Is your servlet implementing SingleThreadModel?


Nope, not using that.
Amit Kumargupta
Ranch Hand

Joined: Apr 13, 2007
Posts: 54
Only one instance is created.

If you implement SingleThreadModel interface the server will create multiple instances of servlet.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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
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.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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
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


Thanks.
Santhosh Reddy
Greenhorn

Joined: Sep 25, 2006
Posts: 27
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,
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
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%
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.

Thanks,
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Santhosh Reddy:




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,


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.

Thanks,
-Ben
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Chengwei Lee:


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
=================================================
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.

Thanks,
-Ben
=====================================================

I will take this into consideration from next time.

Thanks
Prem Kashyap
 
jQuery in Action, 2nd edition
 
subject: Servlet instances and its init method