Results 1 to 7 of 7

Thread: Constructor and init()

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Answers
    1

    Constructor and init()

    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()?


  2. #2
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Re: Constructor and init()

    The initilizer will run regardless of which constructor get called.

    Keep in mind that the initialization happens after the call to the super.constructor and before 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 uninitialized.

    More Information..

    -------------------
    suresh


  3. #3
    Junior Member
    Join Date
    Sep 2007
    Answers
    5

    Re: Constructor and init()

    Quote Originally Posted by pramod.kuber View Post
    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()?
    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


  4. #4
    Junior Member
    Join Date
    Nov 2007
    Answers
    1

    Re: Constructor and init()

    Quote Originally Posted by pramod.kuber View Post
    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()?
    .
    JDk1.0 does not support constructor to accept arguments Servlet Config.


  5. #5

    Re: Constructor and init()

    Quote Originally Posted by pramod.kuber View Post
    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()?
    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


  6. #6
    Junior Member
    Join Date
    Dec 2011
    Answers
    3

    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.


  7. #7
    Junior Member
    Join Date
    Dec 2011
    Answers
    3

    Re: Constructor and init()

    The constructor


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact