wood burning stoves 2.0*
The moose likes Servlets and the fly likes init() and init(conf) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "init() and init(conf) " Watch "init() and init(conf) " New topic
Author

init() and init(conf)

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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: 515
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init() and init(conf)
 
Similar Threads
JBoss 4.2.3 Resource Folder Configuration
Best place for .properties file?
init methods of Servlets: need clarification
Loading Servlet On Startup
super.init(...)