Forums Register Login

Multiple Servlet Instance?

+Pie Number of slices to send: Send
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??
+Pie Number of slices to send: Send
Hello Jyothi,

Only one servlet instance will be created,,




All the best

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Vinod,

I think you are right. Two servlet instances will be created. Anyone else to bolster our statement??
+Pie Number of slices to send: Send
Guys,
I just test the following scenario on tomcat 5. Two instances are being created.
+Pie Number of slices to send: Send
Hi Samanta,
How did you tested? How did you come to know two different instances have been created?
Regards,
Sri.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Ulf, you are right!
+Pie Number of slices to send: Send
Ulf has also joined our board of two different instances , any one objecting to this ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Agreed.. there could be multiple servlet instances for such a case
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1012 times.
Similar Threads
how to compile
Reading form parameters
Problem with secure connection and Form Based Authentication
Regarding init parameters
Servlets from an HTML anchor
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:05:35.