This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes why constructor only is not enough for initializing the servlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "why constructor only is not enough for initializing the servlet ?" Watch "why constructor only is not enough for initializing the servlet ?" New topic
Author

why constructor only is not enough for initializing the servlet ?

rajpal b songara
Greenhorn

Joined: Nov 12, 2010
Posts: 6

Hi All,

I want to know why constructor only is not enough for initializing servlet ? i mean to say what ever the init() method do we can put that into a constructor.
so what is the thing we can achieve from init() method but not from constructor.

thanks.
Balaji Vankadaru
Ranch Hand

Joined: May 31, 2013
Posts: 47


We cannot write constructors with arguments in a servlet class. It will throw Exception.

If you do not intend to access servlet context and config its possible to manage your code trough constructor.
rajpal b songara
Greenhorn

Joined: Nov 12, 2010
Posts: 6

Thanks Balaji For Quick Reply,

so from your answer may i consider that 'for just making servletconfig object,servlet API provide init(ServletConfig) method'.
no additional use at container level or any-other around it ?
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

rajpal b songara wrote:Thanks Balaji For Quick Reply,

so from your answer may i consider that 'for just making servletconfig object,servlet API provide init(ServletConfig) method'.
no additional use at container level or any-other around it ?


The container uses the init() method to actually register your class as an actual Servlet . The constructor alone is not enough to achieve this. After the init() method is executed ( and no exception has occured ), the container makes the servlet object and then registers it, after registration with the container , the servlet is then ready to be used and service any request.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Saif Asif wrote:The container uses the init() method to actually register your class as an actual Servlet .

Not correct. A servlet doesn't need to have an init() method at all. It is the declarations in the deployment descriptor that "register" the servlet.

After the init() method is executed ( and no exception has occured ), the container makes the servlet object

Also not correct. How could the init() method be called if the object does not already exist? Servlets are no different from other objects in Java and a method cannot be called on an instance before that instance even exists.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Bear wrote: A servlet doesn't need to have an init() method at all. It is the declarations in the deployment descriptor that "register" the servlet.

So you mean to say that you do not need an init() method at all to initialize a servlet ?

I quote from "Mannings Book for SCWCD"

..
The servlet is initialized after
the init() method returns
..



Now I am confused here.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Saif Asif wrote:So you mean to say that you do not need an init() method at all to initialize a servlet ?

Overriding one of the init() methods is completely optional.

I quote from "Mannings Book for SCWCD"

..
The servlet is initialized after
the init() method returns
..


You are confusing the word initialize with created. The instance is created long before any initialization takes place. The instance cannot be created after calling methods on it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
Balaji Vankadaru wrote:We cannot write constructors with arguments in a servlet class. It will throw Exception.

Not really correct. A servlet can have any number of constructors, but if it has constructors with arguments then you must add a default constructor without arguments, because the compiler will not insert it for you. Only the default constructcor will ever be called, all others are dead weight.

If you do not intend to access servlet context and config its possible to manage your code trough constructor.

Not really correct, either. A servlet's constructor will only be called once, whereas the init and destroy methods can be called multiple times.

Simple rule of thumb: never put a constructor in a servlet.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Bear wrote: You are confusing the word initialize with created. The instance is created long before any initialization takes place. The instance cannot be created after calling methods on it.


Thank you Bear ! Cleared my misunderstanding !
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Ulf Dittmer wrote:Simple rule of thumb: never put a constructor in a servlet.

Quoted for truth. There is no valid reason to do so.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why constructor only is not enough for initializing the servlet ?
 
Similar Threads
Constructor of Servlet
two questions about servlet.
Method called during servlet initialization
Why does a servlet has init method
Servlet Constructor