Re: Constructor and init()
The initilizer will run regardless of which constructor get called.
Keep in mind that the initialization happens [B]after [/B]the call to the super.constructor and [B]before [/B]any code in your constructor. This can make a difference if the super.constructor calls some method that the subclass has overriden which uses gloob. That method will see gloob [B]uninitialized[/B].
[URL="http://forum.java.sun.com/thread.jspa?threadID=480900&messageID=2241219"]More Information..[/URL]
-------------------
suresh
Re: Constructor and init()
[QUOTE=pramod.kuber;18091]plz can anybody tell me,
What is the difference between constructor and init() method of servlet?
Every class has default constructor, and servlets are also user written classes then its constructor gets call and difference between constructor and init()?[/QUOTE]
Hi,
The init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests. Why not use a constructor instead? Well, in JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments. So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface. Also, Java doesn't allow interfaces to declare constructors. This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter. It has to declare another method, like init(). It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException.
vj
Re: Constructor and init()
[QUOTE=pramod.kuber;18091]plz can anybody tell me,
What is the difference between constructor and init() method of servlet?
Every class has default constructor, and servlets are also user written classes then its constructor gets call and difference between constructor and init()?[/QUOTE].
JDk1.0 does not support constructor to accept arguments Servlet Config.
Re: Constructor and init()
[QUOTE=pramod.kuber;18091]plz can anybody tell me,
What is the difference between constructor and init() method of servlet?
Every class has default constructor, and servlets are also user written classes then its constructor gets call and difference between constructor and init()?[/QUOTE]
whenever load the servlet class to servlet container creates object then call the constructor then create ServletConfig object will create after the container call init(-) method.init(-) method internally call init() method.
so in constructor not accessible ServletConfig and ServletContext objects.
in init() method will accessible ServletConfig and ServletContext objects.
ServletContext object directly or indirectly will call ServletConfig object.
using ServletConfig object to call init parameters in web.xml file.
using ServletContext object to call context parameters in web.xml
Re: Constructor and init()
In general java, init() would just be seen as a normal method name. However, in some aspects of java, the point of the medhod is to initialize things.
Re: Constructor and init()