permaculture playing cards*
The moose likes Servlets and the fly likes choosing between init() and a constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "choosing between init() and a constructor " Watch "choosing between init() and a constructor " New topic
Author

choosing between init() and a constructor

Bis Bang
Greenhorn

Joined: Jan 28, 2004
Posts: 15
Hi,
can we do all the initilization in the constructor of a servlet instead of init()??...if not why?
Thanx in advance...


The glory lies not in not falling, but it lies in rising everytime you fall
john smith
Ranch Hand

Joined: Mar 04, 2004
Posts: 75
Servlets do not have constructors - well, not constructors that you can use. Why? Because they are container managed objects, its up to Tomcat (or whatever container you choose) to construct instances of the servlet. You use init() because it represents actions performed during initialization of the servlet, which is different from construction.
Bis Bang
Greenhorn

Joined: Jan 28, 2004
Posts: 15
Hi,
Jhon...I think u r mistaking...A servlet can have a constructor(default).. U try to write a servlet with a constructor and init() it will compile and run..No doubt about that...
[ March 11, 2004: Message edited by: Bis Bang ]
vikasids sharma
Ranch Hand

Joined: Aug 01, 2003
Posts: 157
Hi bis
No doubt u can create constructor in servlet ,servlet will compile and run. but you have to call your constructor explicitly as servlet engine will go only for methods described for life cycle of servlet. So in that case what ever you write in your servlet outside life cycle , it would be treated as a normal method by Servlet Engine which could be invoked explicitly.
Most important point is Servlet instance is to be created by Servlet Engine , constructor never comes into picture in that creation process.
Think of init method in Servlet a replacement for constructor in normal java class.
Originally posted by Bis Bang:
Hi,
Jhon...I think u r mistaking...A servlet can have a constructor(default).. U try to write a servlet with a constructor and init() it will compile and run..No doubt about that...
[ March 11, 2004: Message edited by: Bis Bang ]


Thanks
Vikas Sharma
SCJP(1.4)
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 196
Remember the following :
When the servlet container starts up, the following can happen :
1. the servlet container looks into the deployment desciptor for a servlet definition.
2. the servlet container instantiates the servlet by calling Class.forName(servlet-class).createInstance(), the no-arg constructor is called.
3. the servlet container calls the init(ServletConfig)-method on the servlet instance. The init(ServletConfig) method stores a reference to ServletConfig and calls the init()-method on the servlet intance. The ServletConfig object contains all the initialization parameters specified for this servlet in the deployment descriptor.
4. the servlet container calls the service(ServletRequest, ServletResponse)-method of the servlet as needed for processing client requests.
5. .....

Therefore a no-arg constructor can be specified in the servlet-class,
and the no-arg constructor will be called. So some initialization can be done. Also remember that a servlet can have some specific init-parameters that can be specified in the web.xml for this specific servlet.
These parameters are wrapped into the ServletConfig object, which is available when the init()-method is called.
Therefore initialization of a servlet by using it's initialization parameters should be done in the init()-method.
When I'm not specific enough or you have more questions, just ask.
Aneesha Singh
Ranch Hand

Joined: Jan 14, 2002
Posts: 47
Hi Bis,
All servlets have a default no-args constructor, which gets called before the init(...) method. The Servlet container can't know what sort of parameters you're expecting as part of the servlet-instance construction step, so it has to assume you have a no-arg constructor that's available to create a "default" instance of the servlet.
There are 2 reasons you use the init() method as opposed to using the constructor:
1) The Servlet container can't know what sort of parameters you`re expecting (as part of the servlet-instance construction step), so it has
to assume you have a no-arg constructor to create a "default" instance of the servlet.
2) Because of the above reason, if parameters are needed, a different mechanism needs to be used (getServletContext()) and a slot to do it in (during the init() method after the servlet has been constructed).
I guess its a way of keeping the API as simple as possible, for everyone involved. You could use the default constructor but not parameters from the ServletContext in the constructor (not portably, anyway). Because the constructor of a Servlet (or Filter) doesn't have access to the ServletConfig (or FilterConfig) object, so it cannot acquire initialization parameters, a reference to the ServletContext instance, or anything like that.
Hope that helps.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Bis Bang:
Hi,
Jhon...I think u r mistaking...A servlet can have a constructor(default).. U try to write a servlet with a constructor and init() it will compile and run..No doubt about that...

Can you have construtor with argument and use it for initializing servlet ??
Here comes init() and servletConfig object in to the picture.
As servlets are loaded dynamically by servlet engine, init and ServletConfig are the means to pass information to the servlet as you cant use constructor with arguments for dynamic loading of class.
HTH.


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Bis Bang
Greenhorn

Joined: Jan 28, 2004
Posts: 15
Hi Aneesha and RK.. yeah we can not have constructor with parameter in servlet...If we create a servletconfig object in the difault consructor; can we have the features of init() method in that case!!! or some how there will be any life cycle violation??...
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
Hi Arnold

Remember the following :
When the servlet container starts up, the following can happen :
1. the servlet container looks into the deployment desciptor for a servlet definition.
2. the servlet container instantiates the servlet by calling Class.forName(servlet-class).createInstance(), the no-arg constructor is called.
3. the servlet container calls the init(ServletConfig)-method on the servlet instance. The init(ServletConfig) method stores a reference to ServletConfig and calls the init()-method on the servlet intance. The ServletConfig object contains all the initialization parameters specified for this servlet in the deployment descriptor.
4. the servlet container calls the service(ServletRequest, ServletResponse)-method of the servlet as needed for processing client requests.
5. .....

I want to know the rest of the steps that the container does, like calling service(..., ...) method which calls doGet() or doPost() etc..
In general, I'd like to know "how (or in what order) servlet container calls the servlets methods (life-cycle methods, constructor, etc)". Any link on this also would be greatly appreciated.
Aneesha Singh
Ranch Hand

Joined: Jan 14, 2002
Posts: 47
Hi Bis,
I dont think that can work. You see, the ServletConfig object is created by the Servlet Container and only passed to the init method as an argument. The ServletConfig object contains Servlet parameters and a reference to the Servlet's ServletContext. It wont be logical to create a new object for this in the constructor.
And why would you want to do that anyway?
Cheers!
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
The answer lies in the Servlet Spec (I refer to Servlet 2.3, i.e. J2EE 1.2, pp. 23-24).
"SRV.2.3.1 Loading and Instantiation
The servlet container is responsible for [...] instantiating servlets. [...]
After loading the Servlet class, the container instantiates it for use.
SRV.2.3.2 Initialization
After the servlet object is instantiated, the container must initialize the servlet before
it can handle requests from clients. [...] The container initializes
the servlet instance by calling the init method of the Servlet interface with a
unique (per servlet declaration) object implementing the ServletConfig interface."

In a nutshell:
- Instantation means that a constructor has to be invoked; this is the nature of Java. The spec does not mandate the existence of a public no-arg constructor. This means that if for some (pedantic) reason you contrive a servlet class with either a single private no-arg constructor or a single constructor with args (or a fortiori a single private no-arg constructor), the servlet container is supposed to able to handle it. Which constructor the container may use in the presence of many constructors is up to the container's discretion, but it seems that the no-arg constructor (if any) would be the logical choice.
- Initialization (whatever is within the init() method) will occur after instantation, i.e. after the Servlet object has been constructed. You can't avoid instantation, and, if you have a single public no-arg constructor, it is definite that whatever code in the container will get executed.
Panagiotis.
[ March 15, 2004: Message edited by: Panagiotis Varlagas ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Hi BIs, I need to ask because you have such a unique name, but how did you get that name?
Thanks
Mark


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

Joined: Jan 28, 2004
Posts: 15
Thanx Aneesha and all.... for giving me all the explanation about the topic..
Hey Mark Do U like the name... Its stands "Biswajit(Bis) Bangalore(Bang)" Creativity.....!!!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Very clever.
I was afraid it wasn't your real name and have to jump on you for not following our naming conventions.
Mark
Vasilis Karas
Greenhorn

Joined: Mar 16, 2004
Posts: 24
Didn't realize this discussion was taking place. I posted this question under another topic...Anyhow,
What happens when you create instances of servlets within another class..
I have a class that when instantiated has a member variable which is of type servlet--calling the default constructor. Does the container create another servlet? I'm looking at someone elses code and trying to explain to them that this isn't the way servlets were designed to work..besides pointing them to section 2.2 of the specification (2.3 Servlet API).
What actually happens when they do this?
[ March 18, 2004: Message edited by: Vasilis Karas ]
 
 
subject: choosing between init() and a constructor