aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Multiple Servlet Instance? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Multiple Servlet Instance?" Watch "Multiple Servlet Instance?" New topic
Author

Multiple Servlet Instance?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9521
    
    2

Ranchers,

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

Joined: Dec 20, 2005
Posts: 57
Hello Jyothi,

Only one servlet instance will be created,,




All the best



saivenumadhav
Vinod Iyer
Ranch Hand

Joined: Apr 13, 2006
Posts: 53
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.

Correct me if i am wrong
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9521
    
    2

Vinod,

I think you are right. Two servlet instances will be created. Anyone else to bolster our statement??
Tridib Samanta
Ranch Hand

Joined: Apr 28, 2004
Posts: 128
Guys,
I just test the following scenario on tomcat 5. Two instances are being created.


Thanks, Tridib
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
Hi Samanta,
How did you tested? How did you come to know two different instances have been created?
Regards,
Sri.
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
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
Marshal

Joined: Mar 22, 2005
Posts: 42632
    
  65
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.


Ping & DNS - my free Android networking tools app
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9521
    
    2

Ulf, you are right!
Vinod Iyer
Ranch Hand

Joined: Apr 13, 2006
Posts: 53
Ulf has also joined our board of two different instances , any one objecting to this ?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9521
    
    2

Some text from David Bridgewater's book to support my post!

The web.xml file,

<servlet>
<servlet-name>JspName1</servlet-name>
<jsp-file>/instanceCheck.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>JspName1</servlet-name>
<url-pattern>/jspName1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>JspName2</servlet-name>
<jsp-file>/instanceCheck.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>JspName2</servlet-name>
<url-pattern>/jspName2</url-pattern>
</servlet-mapping>

And the explanation given as below,

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.
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Agreed.. there could be multiple servlet instances for such a case
 
Don't get me started about those stupid light bulbs.
 
subject: Multiple Servlet Instance?