I created a servlet with both init() and init(ServletConfig) methods overridden. I have put a system.out statement in each on of them.
I find that init() is called before init(ServletConfig). Now this I find quite anamolous, since in the init() method I can do getServletConfig() and try to get ServletConfig, which shall only be set in the next method call.
Could you experts help me undertand why is this order of calling. I would think init(SC) should be called before init().
My guess is you were making the call to super.init(config) before your println statement. The super init(config) would then execute, which calls init(). After the super method executed, it would reutrn to your method, where it would then print the line... Hope that helped!
Thanks very much. I was doing exactly that.
I have a related question now :
I have class (myServlet....lets call it class A) which has methods init() and init(SC)
This servlet extends HttpServlet class (lets call it class B) which has init() and init(SC).
Now : container calls A.init(SC)
A.init(SC) calls B.init(SC)
Probably B.init(SC) calls B.init(), but in no way can B.init(SC) call A.init().....do you get my question.....how does the init() method gets called..even in this changed scenario.
Originally posted by Mohan Panigrahi:
[I replaced A and B with class names and numbered the statements. --Ryan]
1. Container calls myServlet.init(SC)
2. myServlet.init(SC) calls HttpServlet.init(SC)
3. Probably HttpServlet.init(SC) calls HttpServlet.init(),
4. but in no way can HttpServlet.init(SC) call myServlet.init()
.....do you get my question.....how does the init() method gets called..even in this changed scenario.
Re: Statements 3 & 4
Yes, HttpServlet.init(SC) can call myservlet.init(); it just calls this.init(). There isn't really one myServlet object and one HttpServlet object. There's just one myServlet object that probably overrides the init() method and uses the version of init(SC) defined in the HttpServlet base class.
Re: Statements 1 & 2
The container makes a new myServlet object and calls init(SC) on it. If you've overridden init(SC) in myServlet, then it calls that version of the method. But if you've done "the right thing" and NOT overridden init(SC), then the HttpServlet version is used, but any reference to "this" in init(SC) refers to the one myServlet object that the container made.
Look here for more info.
However, as Ryan stated, you ca avoid this problem completely if you just override the no arg init() method. In this case the sequence will be as follows:
myServlet (lets call it class A) has the method init())
HttpServlet class (lets call it class B) has the methods init() and init(SC)
If you are still unclear, keep asking questions.
Originally posted by David O'Meara:
From the API:
A convenience method which can be overridden so that there's no need to call super.init(config).
Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
Just two more cents worth from me:
On the one hand, following the convention of overriding init() but never init(SC) disallows doing something like logging the start of init(SC), as in Paul's first message in this thread. On the other hand, it is what future maintenance programmers expect you to do and is therefore safer.