• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Initialization doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct. However we would only care about to refer to servlet1's instance. Wouldn't we?
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok good. Thanks for your input guys.
 
reply
    Bookmark Topic Watch Topic
  • New Topic