wood burning stoves 2.0*
The moose likes Servlets and the fly likes Why using 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 "Why using init() method" Watch "Why using init() method" New topic
Author

Why using init() method

Vasantha Prabha
Ranch Hand

Joined: Oct 02, 2003
Posts: 108
Why we are using init(),init(ServletConfig config) methods to initialize instead of declaring constructor?

Regards,
Sangeetha


Regards,Vasantha<p>Great minds discuss ideas. Average minds discuss events. Small minds discuss people.
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
may be because in constructor u dont get ServletConfig as a parameter.


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Servlets are intended to run in a "servlet container" just like applets run in an "applet container".
A servlet container manages creation of a servlet instance and calls init with a ServletConfig so that you can initialize your code AFTER the container has handled its responsibilities.
Bill
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
I always use the init() method when i have to connect to the database for example...because it wastes time to connect it..Then init() is usefull to do some things before.
Basavaraju Banakar
Greenhorn

Joined: Jul 18, 2002
Posts: 25
init method is called once and before any of the request to the servlet are serviced. all that is required by the servlet that are common across requests are initialized here..like creation og DB connection....


It is impossible to make anything foolproof because fools are so ingenious..Murphy<br />Basu.
Vasantha Prabha
Ranch Hand

Joined: Oct 02, 2003
Posts: 108
ok I accept we need init(ServletConfig config) method to initialize
initparameter.why do we need this init() method.why can't we can use
init(servletConfig config) itself for all initialization things
Regards,
Sangeetha
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
This is to allow your code to be simpler if you need less features of the servlet API.
In some cases, and application will need to access the servlet config during init. In this case it makes sense to use the more complex init(ServletConfig config). Remember, though, that you must make sure to call "super.init(config) before the rest of your init processing - this is often missed, and can lead to all sorts of confusion later.
If your application does not need to access the ServletConfig during init, it makes much more sense to just implement the simpler init() method. By the time the comtainer calls this, it has already done the equivalent of super.init(config) for you, so you don't need to worry about forgetting it.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Engin Okucu:
I always use the init() method when i have to connect to the database for example...because it wastes time to connect it..Then init() is usefull to do some things before.

I hope you don't mean you're creating Connections in the init() and keeping those alife as globals?
That's extremely poor practice and will work only with extremely low load.


42
John Dunn
slicker
Ranch Hand

Joined: Jan 30, 2003
Posts: 1108
if you want you can have your servlet, "xyz" extend a "baseXyz" servlet. The baseXyz can contain the init() and hide alot of that "one-time" set up code. Once you get those init parts working well you can kind of forget about the base class and concentrate on doGet(), etc.


"No one appreciates the very special genius of your conversation as the dog does."
Vasantha Prabha
Ranch Hand

Joined: Oct 02, 2003
Posts: 108
If your application does not need to access the ServletConfig during init, it makes much more sense to just implement the simpler init() method. By the time the comtainer calls this, it has already done the equivalent of super.init(config) for you, so you don't need to worry about forgetting it.
**************************************************************************
Heyyy If the init() method internally call the super.init(config) then
why we need this init(SerlvetConfig config) method???.We can directly get the getInitParameter() in all the method?
Regards,
Sangeetha
 
Don't get me started about those stupid light bulbs.
 
subject: Why using init() method
 
Similar Threads
Servlet initialization
Why no constructors for servlets?
what is the ServletConfig and usage
Difference between init and servlet constructor
init() instead of constructor