Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whats the o/p of this servlet pgm?

 
Binoj Viswanathan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I got unusual o/p!!! when i invoke
super.init(sc);
in second init(sc) its invoking 1st init() method of the same servlet?
how did this happen?
[ January 04, 2004: Message edited by: Michael Ernest ]
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
short version: there is a call to init() in the init(sc) method of HttpServlet
long version:
1. Container calls SecondServlet.init(sc),
System.out.println(" here(sc) :");
b =14;
c=22;
2. super.init(sc); // this is not good calls the HttpServlet.init(sc)
3. HttpServlet.init(sc) calls init(), but since SecondServlet has overriden the init() method, it will call SecondServlet.init() instead. (This API implementation, you cannot change...)
System.out.println(" here :");
a =15;

c=20;
4. SecondServlet.init() calls super.init();// this is not good , which did nothing and return
5. SecondServlet.init() finishes by System.out.println(" here1 :");
6. HttpServlet.init(sc) finishes and returns.
7. SecondServlet.init(sc) finishes by
System.out.println(" here(sc)1 :");
So, during initialization, msg printed on console will be:
here(sc)
here
here1
here(sc)1
and a = 15, b = 14, c = 20
You have to know this just for the exam(No one knows what will appear in exam), but in practice you will either
1. override the init(sc) method and make a call to super.init(sc) within your init(sc) method. (usually at the beginning)
OR
2. override the init() method, and use getServletConfig() to get a reference to the ServletConfig object if/when necessary.
But not both of them.
[ January 03, 2004: Message edited by: Yi Meng ]
 
Binoj Viswanathan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the same o/p on browser ala console!!!

Thank you so much ur explantion.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added UBB CODE tags to the first post to improve readability.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic