• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding both versions of init() method

 
Mohan Panigrahi
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
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().

Thanks,
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container calls init(servletConfig config) first, which in turn calls the init() method. Here is my code and output:


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!
 
Mohan Panigrahi
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Paul,
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.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably B.init(SC) calls B.init(), but in no way can B.init(SC) call A.init()

B.init(SC) calls A.init(), not B.init(). This is because A.init() overrode B.init(), so the only way to call B.init() is with super.init() from A.


-Yuriy
 
Ryan McGuire
Ranch Hand
Posts: 1055
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohan Panigrahi:

[I replaced A and B with class names and numbered the statements. --Ryan]
Now :
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.

Cool?
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yuriy was correct, B.init(SC) calls A.init(), not B.init() because B.init() was overriden.

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)
  • Container calls A.init(SC), which it inherited from B
  • The code in A.init(SC) executes. Here, A.init(SC) does its initialization work, then makes a call to this.init(). Notice that even though the method was inherited from B, it is being executed within A, so all references to this point to A
  • The code in A.init() executes.


  • If you are still unclear, keep asking questions.
     
    David O'Meara
    Rancher
    Posts: 13459
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    From the API:

    init()
    A convenience method which can be overridden so that there's no need to call super.init(config).

    init(ServletConfig config)
    Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
     
    Ryan McGuire
    Ranch Hand
    Posts: 1055
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by David O'Meara:
    From the API:

    init()
    A convenience method which can be overridden so that there's no need to call super.init(config).

    init(ServletConfig 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.
     
    Consider Paul's rocket mass heater.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic