• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Concepts  RSS feed

 
Ani joshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,
I am new to Servlet. I am confused with one Servlet Concept.
Take an example.
I have a Servlet called BaseServlet which is getting extended from HttpServlet Class.
All other servlets are getting extended from BaseServlet. e,g Servlet1,Servlet2,Servlet3 etc.
When a request comes for Servlet1, I understand that Container will create instance of servlet1 and BaseServlet.
Is it right? OR how it works?
Now request has come for Servlet2. Container will create instance of Servlet 2. Will it create instance of BaseServlet again?
OR will it use the same instance of BaseServlet which was created while creating Servlet1?
If container is creating instance of BaseServlet again then its violating servlet instantiation concept,
as we know there must be only one instance of Servlet.
If container is using the existing instance, How it manages the concurrency issues?
Please reply,
Ani
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The concept that you are confused with isn't specific to Servlets it's related to your understanding of Java instantiation.
When you instantiate an object in Java you are instantiating a single object regardless of it's class hierarchy. Therefore in your example if you instantiate Servlet1 and Servlet2 you get a single instance of Servlet1 and another of Servlet2 with NO instances of BaseServlet.
 
Ani joshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i have some code in constructor of superclass, it gets executed as soon as I create object of its subclass. That means instance of superclass must be getting created.
 
vishal avad
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ani
Constructors r responsible for initialization of a class and not instantiation. So they r bound to get fired when subclass constructor is fired by which the members of the servlet gets initialized but the same instance is used throught.
HIH
Cheers
Vishal
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!