This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Servlet Initialization doubt?  RSS feed

 
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!
 
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!
 
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.
 
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?
 
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
Boost this thread!