Instances of the Servlet

I guess it's more theoretical question.

What is happening when there are multiple requests from the client to the same servlet.

My understanding is that if the servlet implements SingleThreadModel interface then container creates new instance of the servlet per request. The question here is how many instances can the container create, is there any limitations except of the memory size. Is it possible to specify the amount of instances to be created?

What is happening when servlet doesn't implement SingleThreadModel? How does the container handle simultaneous requests from multiple clients? Is there some kind of queue being created by servlet container?

thanks in advance.

[712 byte] By [vs777a] at [2007-11-27 1:01:36]
# 1

> The question here is how many instances can the container create

Servlet specification (ver 2.4) says "the servlet container may

instantiate multiple instances". So there is no standardized rule about how many there are. Check you servlet container's manual if it explains how it is implemented and if you can configure its behaviour (e.g. size of object pool if the container has a pool).

SingleThreadModel is deprecated, you should not use it. Don't put instance variables in servlets (except "static final" manifest constants). Synchronize access to any shared data structures. Then you won't care about SingleThreadModel.

> What is happening when servlet doesn't implement SingleThreadModel?

> How does the container handle simultaneous requests from multiple clients?

It calls theservletobject.doPost() with the same theservletobject object. There is only one object of each servlet. Many requests can simultaneously execute using the same servlet object.

Don't put instance variables in servlets. Then you won't care how many instances of the servlet object there are. There is one, but you won't care. Simultaneous threads won't mess each other's instance variables because there are no instance variables.

sjasjaa at 2007-7-11 23:36:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...