File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Constructor of Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Constructor of Servlet" Watch "Constructor of Servlet" New topic
Author

Constructor of Servlet

Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

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

Joined: Jan 10, 2002
Posts: 61766
    
  67

But again, just because you can, dosen't mean you should. And in this case, you shouldn't.
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

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

Joined: Jan 10, 2002
Posts: 61766
    
  67

Only if you want to look uninformed.
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

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

Joined: Jan 10, 2002
Posts: 61766
    
  67

I would guess that they wanted you to talk about servlet initialization using the ServletConfig instance.
k space
Ranch Hand

Joined: Jun 25, 2002
Posts: 104
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.

SCEA | SCBCD | SCWCD | SCJP - The kSpace
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

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
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

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

Joined: Jun 25, 2002
Posts: 104
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

Joined: Jul 28, 2010
Posts: 153

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

Joined: Jun 25, 2002
Posts: 104
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

Joined: Jul 28, 2010
Posts: 153

Thanks k space and all. My doubt got cleared Thanks all once again. We can close this topic
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor of Servlet