On Fri, 23 Mar 2001, Pradeep Kumar wrote:

> The Servlet specification says that, that the container should
> guarantee only one instance of the servlet. However some of the
> container do maintain a small of pool of servlet instances and
> manage them. For example there are 3 instances of a servlet in the
> pool, and if there are 300 requests, each instance spawns off 100
> threads for 100 requests.  I don't know how far this is true with
> Tomcat. In general you should not make any assumptions about the
> number of instances of your servlet.

Actually, starting with the 2.2 spec, the spec is pretty specific
about the number of instances of each servlet that should be created.
Basically, if the servlet doesn't implement SingleThreadModel, it's
one instance per servlet definition; if it does implement
SingleThreadModel, then a pool of instances may be created to offset
the performance penalty (but there are many good reasons that you
shouldn't implement SingleThreadModel -- basically, it's misleading,
and doesn't necessarily guarantee thread safety, and it's not a clean
solution -- best is to design your servlet to be thread safe, adding
synchronization where needed -- this has been discussed extensively on
sun's servlet-interest mailing list).


>  -----Original Message-----
> From:         Milt Epstein [mailto:[EMAIL PROTECTED]] 
> Sent: Friday, March 23, 2001 8:17 PM
> To:   'Tomcat'
> Subject:      Re: Why is my init method run twice?
> 
> On Fri, 23 Mar 2001, [iso-8859-1] Helgesen  Hans J�rgen wrote:
> 
> > I'm new to this list, so please forgive me if this question has been raised
> > before....
> > 
> > I'm running Tomcat 3.2.1, Apache 1.3.12, java 1.2.2 and Redhat 6.2.
> > 
> > I have a servlet to which I want to pass a single parameter via the web.xml
> > file (below). I'm obtaining the parameter value by calling
> > config.getInitParameter() in the init method of the servlet.
> > 
> > This seems to work fine, the init method is called and the value obtained
> OK
> > when Tomcat is started.
> > 
> > The problem is that when the first request for the servlet arrives, the
> init
> > method is run A SECOND TIME. This time the init parameter IS NOT AVAILABLE.
> > 
> > My theory is that Tomcat calls the init method once for each thread, but if
> > that is true, I would expect Tomcat to pass the init parameters each time?
> > 
> > What is wrong?
> 
> How are you calling the servlet?  I.e., via what URL?
> 
> Note that you can have different servlet definitions (servlet-name)
> for the same servlet (i.e. servlet-class), and they are considered
> different "instances", such that multiple instances of the servlet
> class will be created (meaning init() will be called multiple times),
> and only the parameters specified for each definition will be
> available to that instance.  It looks like you only have one servlet
> definition, but still, if you call the servlet via the class name
> directly, it will create a separate instance.  E.g., from what you
> have below, if you call /servlet/MyServlet and
> /servlet/no.fellesdata.MyServlet, two separate instances will be
> created.
> 
> 
> > BTW, I can't get the servlet-mapping to work either. Any obvious errors?
> 
> What exact URLs are you trying?  What happens when you try them?
> Perhaps you haven't specified the appropriate directives in your
> apache/tomcat conf file to tell apache to pass it to tomcat for that
> URL.
> 
> 
> > Here is my web.xml file:
> > ------------------------
> > <!DOCTYPE web-app
> >     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
> >     "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
> > 
> > <web-app>
> >   <display-name>My test application</display-name>
> >   <description>
> >     Description of my test application
> >   </description>
> > 
> >   <servlet>
> >     <servlet-name>MyServlet</servlet-name>
> >     <description>
> >       Description of my servlet
> >     </description>
> > 
> >     <servlet-class>no.fellesdata.MyServlet</servlet-class>
> > 
> >     <init-param>
> >       <param-name>ConfigFile</param-name>
> >  
> >
> <param-value>/usr/local/jakarta-tomcat/webapps/poes/WEB-INF/myservlet.config
> > </param-value>
> >     </init-param>
> > 
> >    <load-on-startup>5</load-on-startup>
> >   </servlet>
> > 
> >   <servlet-mapping>
> >     <servlet-name>MyServlet</servlet-name>
> >     <url-pattern>/MyServlet</url-pattern>
> >   </servlet-mapping>
> > 
> > </web-app>
> > 
> > 
> > And here is part of my servlet code:
> > ------------------------------------        
> >     public void init (ServletConfig config) throws ServletException
> >     {
> >     super.init(config);
> >     ....bla-bla-bla.....
> >     // Load configfile if it exists.
> >     configfile = config.getInitParameter("ConfigFile");
> >     if (null == configfile) {
> >       configfile = "default.value";
> >     }
> >     }
> >     
> > 
> 
> Milt Epstein
> Research Programmer
> Software/Systems Development Group
> Computing and Communications Services Office (CCSO)
> University of Illinois at Urbana-Champaign (UIUC)
> [EMAIL PROTECTED]
> 
> 
> 

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]

Reply via email to