Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor of Servlet

 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • 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 ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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).
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2332
86
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Java Oracle
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if you want to look uninformed.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess that they wanted you to talk about servlet initialization using the ServletConfig instance.
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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
Java Oracle
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Pie
Posts: 2332
86
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks k space and all. My doubt got cleared Thanks all once again. We can close this topic
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic