You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Evan J <ma...@gmail.com> on 2007/03/30 05:20:28 UTC
Number of Servlets Tomcat Container Creates
Hi,
I am new to servlets and web container. From what I have read so far,
I've gotten an impression that the web containers, in this case
Tomcat, create only _one_ instance of each particular servlet upon
start/[re]deployment of a web application. Any request to such servlet
would cause a container to spawn a thread that would call upon this
servlet instance's methods (service()...).
My first question is, that under any circumstances, does the container
create a second instance of a servlet (obviously we are talking about
a servlet with a unique registered name not all the servlets in the
context)? I tried to verify this by placing `this' in a Hashtable
during doGet/Post() call to see whether the Container creates a new
one which apparently doesn't but I just want to make sure if my
assumption is correct. If that's correct then it does not matter the
servlet's variable be an instance variable or a class variable.
Another question that arises (relating to the first question), does
every newly created thread with the help of Container get accessed to
that single servlet instance and pass the request/response objects to
the servlet?
Wanting to dig deeper into details, and I know this may not be an
appropriate mailing list, but where does Tomcat container management
actually create these threads? What java file to be more specifically?
Thank You
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Number of Servlets Tomcat Container Creates
Posted by Evan J <ma...@gmail.com>.
>
> I wouldn't get too hung up on the internals of tomcat here. You should
> remember servlet engines can be implement the spec in any way they see
> fit. They are only required to comply with the spec. Anything not
> spelled out there is subject to interpretation.
>
> Suffice it to say servlets in and of themselves should be as stateless
> as possible as various threads call on their methods. Stuff that
> persists for the life of the request, session, or application are stored
> as attributes of the request, session, or servletContext respectively.
>
> I'll step back here and let a tomcat developer explain the internals of
> tomcat. I've only ever had a cursory look at the tomcat internal source
> code. I do know the threads aren't created per request -- they are
> pooled and the pool creates/drops threads as necessary. Everywhere else
> they are just borrowed, used, and returned.
>
> --David
You are correct in saying Tomcat runs a container-managed thread pool
but I was just wondering what kind of pooling mechanism it utilizes.
Tomcat appears to be not only implemented elegantly but confiding
itself in simplicity.
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Number of Servlets Tomcat Container Creates
Posted by David Smith <dn...@cornell.edu>.
Evan J wrote:
> On 3/30/07, David Smith <dn...@cornell.edu> wrote:
>
>> Quoting the spec here (SRV.2.1 of servlet spec 2.4):
>>
>> "The handling of concurrent requests to a Web application generally
>> requires that the Web Developer design servlets that can deal with
>> muiltiple threads executing within the service method at a particular
>> time.
>>
>> "Generally the Web container handles concurrent requests to the same
>> servlet by concurrent execution of the service method on different
>> threads."
>>
>> That right there suggests strongly that you should avoid class instance
>> variables.
>>
>> On the thread front, the threads I believe are created by the connectors
>> to handle incoming requests. Connector threads aren't tied to any
>> specific webapp or servlet. They call the appropriate method of a
>> servlet and pass in a HttpServletRequest and HttpServletResponse object.
>>
>>
>> --David
>
>
> David,
>
> Thank you for your response. A further investigation of Tomcat source
> code reveals that, indeed, the servlet definition class such as
> `org.apache.catalina.core.StandardWrapper.java' implements a singleton
> design in creation of each servlet (in allocate() method). Unless of
> course, servlet implements a single thread model, in that case, a
> stack structure is used to handle pool of such servlet instances.
>
> What I am still inclined to figure out is at what location(s) does the
> container creates threads to handle servlet requests/responses? I've
> looked in `org.apache.catalina.core/connector' packages in vein. Does
> anyone aware of such detail?
>
> Thank You
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
I wouldn't get too hung up on the internals of tomcat here. You should
remember servlet engines can be implement the spec in any way they see
fit. They are only required to comply with the spec. Anything not
spelled out there is subject to interpretation.
Suffice it to say servlets in and of themselves should be as stateless
as possible as various threads call on their methods. Stuff that
persists for the life of the request, session, or application are stored
as attributes of the request, session, or servletContext respectively.
I'll step back here and let a tomcat developer explain the internals of
tomcat. I've only ever had a cursory look at the tomcat internal source
code. I do know the threads aren't created per request -- they are
pooled and the pool creates/drops threads as necessary. Everywhere else
they are just borrowed, used, and returned.
--David
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Number of Servlets Tomcat Container Creates
Posted by Evan J <ma...@gmail.com>.
On 3/30/07, David Smith <dn...@cornell.edu> wrote:
> Quoting the spec here (SRV.2.1 of servlet spec 2.4):
>
> "The handling of concurrent requests to a Web application generally
> requires that the Web Developer design servlets that can deal with
> muiltiple threads executing within the service method at a particular time.
>
> "Generally the Web container handles concurrent requests to the same
> servlet by concurrent execution of the service method on different threads."
>
> That right there suggests strongly that you should avoid class instance
> variables.
>
> On the thread front, the threads I believe are created by the connectors
> to handle incoming requests. Connector threads aren't tied to any
> specific webapp or servlet. They call the appropriate method of a
> servlet and pass in a HttpServletRequest and HttpServletResponse object.
>
>
> --David
David,
Thank you for your response. A further investigation of Tomcat source
code reveals that, indeed, the servlet definition class such as
`org.apache.catalina.core.StandardWrapper.java' implements a singleton
design in creation of each servlet (in allocate() method). Unless of
course, servlet implements a single thread model, in that case, a
stack structure is used to handle pool of such servlet instances.
What I am still inclined to figure out is at what location(s) does the
container creates threads to handle servlet requests/responses? I've
looked in `org.apache.catalina.core/connector' packages in vein. Does
anyone aware of such detail?
Thank You
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Number of Servlets Tomcat Container Creates
Posted by David Smith <dn...@cornell.edu>.
Quoting the spec here (SRV.2.1 of servlet spec 2.4):
"The handling of concurrent requests to a Web application generally
requires that the Web Developer design servlets that can deal with
muiltiple threads executing within the service method at a particular time.
"Generally the Web container handles concurrent requests to the same
servlet by concurrent execution of the service method on different threads."
That right there suggests strongly that you should avoid class instance
variables.
On the thread front, the threads I believe are created by the connectors
to handle incoming requests. Connector threads aren't tied to any
specific webapp or servlet. They call the appropriate method of a
servlet and pass in a HttpServletRequest and HttpServletResponse object.
--David
Evan J wrote:
> Hi,
>
> I am new to servlets and web container. From what I have read so far,
> I've gotten an impression that the web containers, in this case
> Tomcat, create only _one_ instance of each particular servlet upon
> start/[re]deployment of a web application. Any request to such servlet
> would cause a container to spawn a thread that would call upon this
> servlet instance's methods (service()...).
>
> My first question is, that under any circumstances, does the container
> create a second instance of a servlet (obviously we are talking about
> a servlet with a unique registered name not all the servlets in the
> context)? I tried to verify this by placing `this' in a Hashtable
> during doGet/Post() call to see whether the Container creates a new
> one which apparently doesn't but I just want to make sure if my
> assumption is correct. If that's correct then it does not matter the
> servlet's variable be an instance variable or a class variable.
>
> Another question that arises (relating to the first question), does
> every newly created thread with the help of Container get accessed to
> that single servlet instance and pass the request/response objects to
> the servlet?
>
> Wanting to dig deeper into details, and I know this may not be an
> appropriate mailing list, but where does Tomcat container management
> actually create these threads? What java file to be more specifically?
>
> Thank You
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org