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 one query regarding 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 "one query regarding init() method" Watch "one query regarding init() method" New topic
Author

one query regarding init() method

Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Can someone explain in detail below given statements..

"we use the constructor, instead of init(), to initialize servlet. But you will not get the servlet specific things from constructor. The original reason for init() was that ancient versions of Java couldn�t dynamically invoke constructors with arguments, so there was no way to give the constructor a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won�t have access to a ServletConfig or ServletContext. "
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
This statement misses the point. Interfaces are used in Java to force an implementing object to have certain methods. "init(ServletConfig)" is part of the Servlet interface; that's where I can get the ServletConfig object if I need it. But the Java compiler can't force a class to have a constructor with a particular signature; so that would be an inferior approach that would introduce an additional error scenario for the container to handle.

As an aside, HttpServlet's "init()" method wasn't added until a later version of the Servlet API as a convenience method for those cases where a ServletConfig object wasn't required.

Servlets aren't the only place that work this way. Applets, too, are constructed by a container, and then their "init" method is called to let the applet code do any initialization that's required.
[ August 13, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
thanks a lot for providing this information :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one query regarding init() method
 
Similar Threads
choosing between init() and a constructor
why init() method is not final?
Can we write constructor for Servlets?
Init() Vs Constructor
which method call first?