• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet hierarchy

 
Fernando Galdino
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone here could explain for me what happen when I do some servlet hierarchy?

for example:
I have a MainServlet, than others servlets extends this servlet. Every submit will happen for a servlet that exetends MainServlet.
What I will have in server memory? for each servlet I will have a instance of MainServlet or just one instance of MainServlet for every servlet?

thanks
 
unni krishna
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet hierarchy is just like ordinary class hierarchy.
If you have class B extend Class A, and if you create
B b1 = new B(), only B.class will get loaded in memory and
this B.class will have all the inherited properties from A.
So in your case, only one instance of the servlet will be
there in memory and that instance will be that of the
derived servlets type.
Guys one pls correct me if I am wrong.
 
Fernando Galdino
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it's a doubt about OO or J2SE, but in this case when you construct an object of "B", the compiler will invoke the superclass constructor, then will the superclass not be alocated in the memory? but their static content will be initialized?

In the case that I asked about servlet hierarchy, in the superclass MainServlet I override the init method. For each children of MainServlet the init method will be invoked?


thanks
 
unni krishna
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just run this program and see,

public class child extends parent {

static {
System.out.println("Child Loaded into memory.");
}

static int instanceCount;

child() {
instanceCount++;
}

public static int getInstanceCount() {
return instanceCount;
}

public static void main(String[] args) {
child c1 = new child();
child c2 = new child();

System.out.println("No of Parent Instance :" + parent.getInstanceCount());
System.out.println("No of Parent Instance :" + child.getInstanceCount());
}
}


class parent {

static {
System.out.println("Parent Loaded into memory.");
}

static int instanceCount;

parent() {
instanceCount++;
}

public static int getInstanceCount() {
return instanceCount;
}
}

My previous answer is wrong. For each derived class instance one parent class instance also will be created. This is applicable to servlets also.
 
Mark Spritzler
ranger
Sheriff
Posts: 17290
9
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"fgm"

Welcome to Javaranch. Unfortunately your name does not meet our Naming Policy. Please click on the "My Profile" link underneath the "Post a Reply" near the top right of the screen, and change your name, so that it includes your real first and last name.

Thanks and have fun.

Mark
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!