This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlet constructor & init method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet constructor & init method" Watch "servlet constructor & init method" New topic
Author

servlet constructor & init method

manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Hi

I have read a lot on this topic in the forums but never got a conclusive answer.

Say I have some initialization functionality. Say i decide to initialize the same in the servlet's no args constructor instead of the regular init method. Both are called only once during the life time of the servlet.

So whats the advantage of having all inside a init method.
To be precise what i want to know what were the perils of the no args (not parameterized)servlet constructor approach which caused the creators of servlets to resort to the init method approach.

Is there some handshake between the container & the init method which is not in the case of servlet's no arg constructor

Do ellaborate a bit

Bye
Manish
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

It is guarenteed that the servletConfig instance is set up when the init() method is called.

When it comes down to it, it's always best to do things in the standard manner. Just because you might seem to be able to get away with something like setting up in a constructor, that's no guarentee that things won't break later when an underlying assumption changes.

It's your foot; you can choose whether to shoot at it or not.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: servlet constructor & init method
 
Similar Threads
Servlet
Destroy Method in servlet constructor
Servlet's init() and destroy() method and constructors
Story of a Servlet ?
interview questions - J2EE