aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why Init Method, why can't we use constructor instead? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why Init Method, why can Watch "Why Init Method, why can New topic
Author

Why Init Method, why can't we use constructor instead?

Guhan NS
Greenhorn

Joined: Jan 12, 2005
Posts: 1
Hi,

Why is the init method there in servlet class? Why can't we do all that we can do it in init, in constructor itself?

Thanks,
Guhan
Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167
I would assume that the init method is for initializing the environment for the servlet to operate in.

init would have access to the servletconfig as well as servletcontext, so that you could set session or request attributes using them or so operations like open database connections.

With the constructor it the object creation only that is done.

It is the way Java is structured for servlets.
moto raja
Ranch Hand

Joined: May 28, 2005
Posts: 55
Hi Guhan,

First thing is that the constructor of the servlet is called by the container, we cannot call it. So we cannot add anything into the constructor.Moreover, at that time, it is just another java objectnot a servlet.So it cannot have any servlet specific parameters,like ServletConfig , which we can use.

For example,for every request from the clinet, the servlet has to look into the database(or something of that sort),so we have to set up the database connection set-up,login etc. for every request.

We have to make the servlet ready to have a connection to the database/resource before any client requests come.
How can we do it?Sure,it cannot be put in the constructor.

The init() method has access to the ServletConfig,ServeltCOntext,which can be used for similar purposes,for example, use ServeltContext to get database name,login name ,etc.. (that are defined in xml's...)..

To perform all the above melodrama, we should override the init() method and put the code in that.


Now I suppose u understand why contructor() is not sufficient in these kind of cases,hence init() can be overriden in such cases.


:-)
moritala.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

First thing is that the constructor of the servlet is called by the container, we cannot call it. So we cannot add anything into the constructor


The init method is also called by the container. You can write a non-argrument constructor having initialization code, but is is bad parctice. While loading, there is something in betwwen the constructors and init method ( initialization of some objects like ServletConfig) so we can get access to initalization parameters to servelts, ServletContext etc. So it it easier to write initalization codes in init method

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Init Method, why can't we use constructor instead?