aspose file tools*
The moose likes Servlets and the fly likes more than one instance of servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "more than one instance of servlet" Watch "more than one instance of servlet" New topic
Author

more than one instance of servlet

Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
"It is important to note that there can be more than one instance of a given Servlet class in the servlet container. For example,

1) If there was more than one servlet definition that utilized a specific servlet class with different initialization parameters

2) by implementing SingleThreadModel interface.

can someone help me in understanding point 1.

thanks
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

ahh i think two mapping of same servlet in web.xml with different init-param parameter.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
As explained by Sunny the case 1) will occur when you have different servletConfig parameters for same servlet class.
2) situation will come if you implement singleThreadModel interface. If you are implementing this interface then it means you are telling the container that you want only one thread to access the servlet object at a given time. Container ensures this by pooling the object of servlet class. Hence you will have more than one instance of the same servlet and container assigns different instances to different threads,
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
For the first point .. what i come to find by now is ......

Some fragment from web.xml
-------------------------------------------------------
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
<init-param>
<param-name>Name</param-name>
<param-value>Brij</param-value>
</init-param>
</servlet>

<servlet>
<servlet-name>firstservlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
<init-param>
<param-name>Name</param-name>
<param-value>Garg</param-value>
</init-param>
</servlet>

--------------------------------------------------------------
I think this is the example where we have 2 instances for the same servlet.
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
Yes thats correct
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Originally posted by bittoo garg:
For the first point .. what i come to find by now is ......

Just a few points:
  • It's not a great idea to use the same names, but with different casing. That would be very confusing.
  • Always be sure to put your classes in a package other than the default.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
     
    Consider Paul's rocket mass heater.
     
    subject: more than one instance of servlet
     
    Similar Threads
    can there ba more than one instance of servlet of a servlet at one time?
    why cant there be multiple servlets running at the same time?
    Is Servlet Singelton
    How is Servlet a singleton?
    when init() is being called?