wood burning stoves 2.0*
The moose likes Servlets and the fly likes Constructor in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Constructor in servlets" Watch "Constructor in servlets" New topic
Author

Constructor in servlets

shivanath yerra
Greenhorn

Joined: Jan 17, 2002
Posts: 14
Can we have constructor in servlets? if so what is the need for init method. What are all the initilization done in init method can be done in a constuctor also then what is the need for an init method.
Prasad Charasala
Ranch Hand

Joined: Nov 02, 2000
Posts: 67
Shiva,
The purpose of Constructor and Init method in Servlets is different. The constructor is called whenever you create a new object.
Init method is called when the container loads the servlet for the first time. Init method is called only once when the servlet is loaded. The only similarity between constructer and init method is that they both are called only once. Thats it, similarity ends there.
Init method is like any other mehod in a Thread class but service method is like run method in Thread class.


<B>Prasad</B>
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
shiva,
Javaranch has a naming policy. Please re-register with a name that complies with that policy so that you may continue to post here. Thanks.


Matthew Phillips
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
The convention is that the init method is where the servlet gets information on the servlet config object and servlet context. One form of the init method is:
public void init(ServletConfig config)
throws ServletException
that is in the javax.servlet.GenericServlet class.
As a matter of convenience, and because people kept forgetting to call super.init( config ), there is also the
public void init()
method. When you override init() the servlet container really calls the method that takes the ServletConfig in GenericServlet - that method saves the ServletConfig and calls init() in your servlet. Thus your servlet gets the ServletConfig information without you having to specifically code it.
Bill
shivanath yerra
Greenhorn

Joined: Jan 17, 2002
Posts: 14
Hi William,
Thanx for your reply. But William if i overload a constructor and pass servletconfig object to one of the overloaded constuctor then also i can access the servletconfig object. yha we have two methods init() and init(servletconfig).for an object we too can provide a default and a parameterized constuctor for which we can have something like object() and object(servletconfig) then what is the need for an init() method
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
And exactly how do you propose to get hold of a valid ServletConfig in order to call this constructor?
You seem to be missing the point about the environment servlets run in. The servlet container is responsible for setting up the environment, and takes care of a lot of other details - thats why the servlet API is so easy to work with.
What is it that you think you need a constructor for that can't be done the normal servlet way?
Bill
Zakaria Haque
Ranch Hand

Joined: Jan 02, 2002
Posts: 60
Originally posted by Prasad Charasala:
Init method is like any other mehod in a Thread class but service method is like run method in Thread class.

Well, from the JVM's perspective java constructors are just another method as well. see section 3.9 of VM spec
I think the main difference between init() and a default constructor is you have acees to a valid servlet config object from init(), but not from the constructor.
Another difference is, constructor is called when instantiating the servlet. But init() will be called sometimes between instantiation and the first call to service(). depending on your appserver (and its configuration) there can be significant delay between these two. So, if your servlet instance needs to hold some resources, it does not make sense to do it from the constructor.
And finally, it is better to follow a standard. Rather than having your initialization code scattered among constructor and init(), its better to have at one standardized place, which is init()
Hope it helps.


tobe bondhu nouka bherao<br />shonabo gaan aj shara raat
shivanath yerra
Greenhorn

Joined: Jan 17, 2002
Posts: 14
hi,
as you said init() enable us to access servletconfig if the servletcontext passes the servletconfig to the object instead of init() method then also we can achive the same result.
as the second point of object creation when the request comes then we can create an object of the servlet and make it available. my question is why should we have init() for initilization where the same can be done in a constuctor also and we can pass servletconfig object as
xyz(servletConfig).
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
Why do we have init instead of a constructor?
Well, probably because having a no-args constructor makes it easy for the servlet engine to construct a servlet using the String you pass in the web.xml file, using
Class.forName( yourclassname) ;
Bill
 
wood burning stoves
 
subject: Constructor in servlets
 
Similar Threads
init() and constructor combined?
Why Init() method
Why be use init method
Servlet init() vs constructor
getter and setters