• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Constructor of Servlet

 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why I can't write my own constructor in Servlet ?what are the consequence if I write it?

Can any body please explain ?
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Malatesh Karabisti wrote:why I can't write my own constructor in Servlet?


You can. What is preventing you?

what are the consequence if I write it?


Everyone will think that you don't know what you are doing.

Can any body please explain ?


The init() method is the proper place to initialize a servlet as the ServletConfig instance has been established by then. Constructors are not useful and are not usually used (I have, in fact, never written a servlet constructor in over a decade of writing servlets).
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Malatesh,

As Bear is mentioning: you can as long as you provide a no-argument constructor otherwise you will get an exception (java.lang.InstantiationException)

Regards,
Frits
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But again, just because you can, dosen't mean you should. And in this case, you shouldn't.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The init() method is the proper place to initialize a servlet as the ServletConfig instance has been established by then. Constructors are not useful and are not usually used



Ok initialization should be done in init() method because by the time container calls the init() method, ServletConfig will be initialized properly.

If I don't want do any in the init() method can I write my own constructor ? Please explain
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only if you want to look uninformed.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No my doubt is not clear. This question is asked in one of the interviews what would be the proper answer ?please elaborate.
I gave the reply like we should not write the constructor container will do for us. That is not a proper answer I know.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would guess that they wanted you to talk about servlet initialization using the ServletConfig instance.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Malatesh Karabisti wrote:why I can't write my own constructor in Servlet ?what are the consequence if I write it?



I guess you are talking about non-default constructors.

You can write a servlet that has the default constructor plus other constructors, but only default constructor will be used by the container to instantiate your servlet. Please remember the container only knows about the javax.servlet.Servlet interface. Once the servlet object has been instantiated or created, the container will invoke the init(ServletConfig) method and let the servlet initialize itself before serving any requests. The init method is the proper place for the initialization, as it is able to access the environment through the ServletConfig.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. I could understand that one then If I write a default constructor do I need call the init() method or container will take care of calling this one ?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If I write a default constructor do I need call the init() method or container will take care of calling this one ?


The container will call the init() method

Regards,
Frits
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Malatesh Karabisti wrote:If I write a default constructor do I need call the init() method or container will take care of calling this one ?



Hi Malatesh, I believe you need to understand the Life Cycle of a servlet. Here is one Servlet Life Cycle.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for answers. ok I understood the Life Cycle after running the default constructor the container calls the init() method. My doubt is here. There are two init methods in GenericServlet


public void init(ServletConfig config) throws ServletException
public void init() throws ServletException


In API Of GenericServlet
It is given


public void init() throws ServletException
This method is provided as a convenience so that servlet writers do not have to worry about storing the ServletConfig object. When extending this class, simply override this method and it will be called by GenericServlet.init(ServletConfig config);



Can you please explain this ?
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Malatesh,

The web container only knows about the Servlet interface, which defines the init(ServletConfig) method.

The GenericServlet is a helper class that makes writing servlets easier. The init() method of the GenericServlet (as you stated that) is a convenience method so that there's no need to call super.init(config). Please check the API for the details.

Please also note that you can develop your servlet from scratch, i.e. without extending the GenericServlet class or the HttpServlet class.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks k space and all. My doubt got cleared Thanks all once again. We can close this topic
 
I am going to test your electrical conductivity with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic