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

Servlet Initialization doubt?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I have the following servlet structure,

servlet1 extends servlet2

servlet2 extends servlet3

servlet3 extends HttpServlet

My question here is that when I start my webapp that has all 3 servlets and when my first request is for servlet1, will servlet2 and servlet3 have been executed. Well as per OO terms, servlet2 and servlet3 has to run to exist...

Please help me guys on this!
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as per OO terms, servlet2 and servlet3 has to run to exist...


Inheritance ultimately creates only one class. So creation of derived class no way going to create seperate instance of base classes!
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets would be called but the important point to note is that we are ultimately having a single instance to care for.

Correct me if i am wrong.
 
Akbar Upadyayula
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think servlet2 and servlet3 will be loaded for creation of servlet1 instance, so no need to create servlet2 and servlet3 instances
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I tried it and all 3 servlet instances are created. How can a base class object even exist without it's parent!
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct. However we would only care about to refer to servlet1's instance. Wouldn't we?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
Actually I tried it and all 3 servlet instances are created. How can a base class object even exist without it's parent!


Are you sure?

How did you test it? Did you tried by any chance adding some System.out.println in all servlets constructors? What does it happen when you print something out inside all sevlet classes' init() method?

Don't forget that when instantiating servlet1, its superclasses' (servlet2 and sevlet3) default constructors will be called as well. That doesn't mean that all servlets were created.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct sergio, I tried testing it with using print statements and the fact is that only the class object is created but it is not given it's servletness (like the Head First used to refer...I like it) because we are not calling the init method of the superclasses.
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok good. Thanks for your input guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic