| Author |
init() and init(conf)
|
Dale DeMott
Ranch Hand
Joined: Nov 02, 2000
Posts: 514
|
|
When does init() and/or init(conf) get used when you create a servlet? What calls it. Should I be concerned with overriding this method, or should I count on the constructors calling it. Dale
|
By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
|
 |
Mike Curwen
Ranch Hand
Joined: Feb 20, 2001
Posts: 3695
|
|
init is called for you by the container. The init method in servlets can be thought of like the constructor in any other class. At least that's how I think of it So any variable setting or other chores I want to do when my servlet is first loaded, I do in the init method.
|
 |
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
|
|
Dale The init(Servletconfig conf) method is from the servlet interface, it is called by the web container and passes the config object. You can override it in your servlets but then must remebe to call super.init(ServletConfig). The init() method is provided in the GenericServlet class and is just there as a convienence to the programmer so you dont have to call super.init(ServletConfig). The init() method (if you override it) is called by the init(Servletconfig) method. You get that functionality from the GenericServlet class, so if you were trying to make our own servlet hierarchy by just implementing Servlet and ServletConfig you wouldn't get that ability - then again why would you want to do that hope that helps
|
Dave
|
 |
Dale DeMott
Ranch Hand
Joined: Nov 02, 2000
Posts: 514
|
|
So my question is, do you HAVE to call it? When do you feel it is appropriate to call it. Why is it provided? -Dale
|
 |
Jessica Sant
Sheriff
Joined: Oct 17, 2001
Posts: 4313
|
|
Originally posted by Dale DeMott: So my question is, do you HAVE to call it? When do you feel it is appropriate to call it. Why is it provided?
The init method is called ONCE, when your servlet is first created -- you do ONE TIME setup things here (its not called for each request) -- people often use it to getInitParameters() defined for the servlet...
|
- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
|
 |
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
|
|
To be even more specific:
...do you HAVE to call it?
NO!! The only time you have to call either of the inits is when you override the init(Servletconfig) then you have to call super.init(ServletConfig). If you only have init() then you dont have to do anything - your method is called by the init(ServletConfig) that was, in turn, called by the web container.
|
 |
 |
|
|
subject: init() and init(conf)
|
|
|