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
Joined: Jan 14, 2003
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.
Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Joined: Apr 09, 2003
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.
Joined: Nov 29, 2001
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