Multiple Servlet Instance?

Consider the following scenario. I have a servlet and I have my web.xml configured in such a way that 2 servlet names point to the same servlet. Then in that case, will 2 seperate instances of the servlet be created when I access it using the two url's that I configured??

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!

madhav changala

Ranch Hand

Posts: 57

posted 9 years ago

Hello Jyothi,

Only one servlet instance will be created,,

All the best

saivenumadhav

Vinod Iyer

Ranch Hand

Posts: 53

posted 9 years ago

According to SRV.9.12

Instantiate an instance of each servlet identified by a <servlet> element that includes a <load-on-startup> element in the order defined by the load-onstartup element values, and call each servlet instance�s init() method.

Hence i think declaring two different <servlet> tage with unique name pointing to same class will give you two instance of the servlet , erach corresponding to the one declared in servlet tag.

Hi Samanta, How did you tested? How did you come to know two different instances have been created? Regards, Sri.

Swati Udas

Ranch Hand

Posts: 121

posted 9 years ago

Theorotically I feel only one instance should be created... the service method could be called any number of times by any number of links..but their would be just one instance of the servlet

SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)

Ulf Dittmer

Rancher

Posts: 42969

73

posted 9 years ago

Theorotically I feel only one instance should be created

Consider the case where the two servlet declarations have different init parameters in the web.xml file, which get stored in fields in the servlet code. In order for the two servlets to reflect those different values, they can't be implemented by just a single instance.

The point to note is that just like servlets (and after all, a JSP page is� ultimately�a servlet), each of these three methods of access establishes three separate instances of the servlet class within the web container. Multiple instances will occur like this only when the JSP page is registered in more than one way.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!