It's not a secret anymore!
The moose likes Servlets and the fly likes Init() vs. Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Init() vs. Constructor" Watch "Init() vs. Constructor" New topic
Author

Init() vs. Constructor

Tanzeem Akhtar
Greenhorn

Joined: Sep 11, 2007
Posts: 9

Hi guys, Well can you tell me that why there is need of "init()" , i mean why can not we initialize the servlet object with the help of constructors?
Thank you.


Tanzy..
http://techythought.wordpress.com/
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12271
    
    1
Recall that a servlet is NOT a stand alone application. It has to operate in the context of the servlet container.

Therefore you would either have to always provide a constructor taking the ServletConfig or define an API that uses init() - the convention of a no-args constructor and an init method for objects operating in a container is used in Applets and other Java APIs - predating the servlet API.

So servlets are just following an established pattern.

Bill


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Init() vs. Constructor
 
Similar Threads
Doubt- Init() method
init() method doubt?
Why cann't we initialize servlets in constructor?
Overriding both versions of init() method
init() method