Forums Register Login

init() and init(conf)

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
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.
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 945 times.
Similar Threads
JBoss 4.2.3 Resource Folder Configuration
init methods of Servlets: need clarification
Best place for .properties file?
super.init(...)
Loading Servlet On Startup
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:26:53.