> Now an user calls this servlet ......what happens after this ?
The container checks if an instance is available - if not, it loads the
servlet class (constructor is called), reads the servlet config
parameters (from web.xml), creates a ServletConfig object, and calls
init() passing in the ServletConfig. Once this is done, the servlet is
in "ready" state.
> does an servlet instance is created by the container as soon as user
> calls it ?
It depends. The container can keep a pool of servlets, or it can do a
lazy load (on 1st request) or it can instantiate at startup.
> how those user inputs are passed to the servlet ?
Based on the HTTP request method type (post, get, head, delete, put,
trace...) the appropriate doXXX method is called. From a container
standpoint, it creates 2 objects for every request - HttpServletRequest
and HttpServletResponse. These are then passed to the service() method
of a "Ready" servlet, and the service() method figures out which doXXX
is called.
> say, 5 users calls the servlet at a time , does the container creates 5
> instance at a time ?
No - it creates 5 threads that execute the servlet's service() method.
If the servlet implements SingleThreadModel, only 1 request thread can
execute the service() method at a given time, and others wait.
> what happens when the the business logic is served by the container
> .....does it automatically destroyed by the container ?
No, a servlet gets destroyed when the container process ends. The
service() method just ends, keeping the servlet ready for more request
threads.
> I know the servlet life cycle .
> but how it will work in this situation is a matter of worriness.
[quoted text clipped - 4 lines]
> methods activates ---->then doget/dopost activates ---->process
> business logic in doget/dopost ----->servlet is destroyed.
servlet is not destroyed, the service() method returns.
Hope this helps!
-cheers,
Manish
Juha Laiho - 08 Nov 2006 09:12 GMT
Couple of corrections to what Manish wrote;
"Manish Pandit" <pandit.manish@gmail.com> said:
>> say, 5 users calls the servlet at a time , does the container creates 5
>> instance at a time ?
>
>No - it creates 5 threads that execute the servlet's service() method.
>If the servlet implements SingleThreadModel, only 1 request thread can
>execute the service() method at a given time, and others wait.
Nope; with SingleThreadModel the container is allowed to (and at least
Tomcat does) instantiate multiple copies of the servlet based on the
amount of requests. The "one is served, others wait" is what happens
if you synchronize the service method in the servlet (DON'T DO THAT!).
>> what happens when the the business logic is served by the container
>> .....does it automatically destroyed by the container ?
>
>No, a servlet gets destroyed when the container process ends. The
>service() method just ends, keeping the servlet ready for more request
>threads.
Container is allowed to remove a servlet at any time when there are no
requests being processed by that servlet.

Signature
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Manish Pandit - 08 Nov 2006 10:01 GMT
> Nope; with SingleThreadModel the container is allowed to (and at least
> Tomcat does) instantiate multiple copies of the servlet based on the
> amount of requests. The "one is served, others wait" is what happens
> if you synchronize the service method in the servlet (DON'T DO THAT!).
>From Servlet Spec 2.4, page 219:
SRV.14.2.24 SingleThreadModel
public interface SingleThreadModel
Deprecated. As of Java Servlet API 2.4, with no direct replacement.
Ensures that servlets handle only one request at a time. This interface
has no
methods. If a servlet implements this interface, you are guaranteed
that no two threads will
execute concurrently in the servlet's service method. The servlet
container can
make this guarantee by synchronizing access to a single instance of the
servlet, or
by maintaining a pool of servlet instances and dispatching each new
request to a
free servlet.
-cheers,
Manish
Lew - 12 Nov 2006 16:39 GMT
>> Nope; with SingleThreadModel the container is allowed to (and at least
>> Tomcat does) instantiate multiple copies of the servlet based on the
>> amount of requests. The "one is served, others wait" is what happens
>> if you synchronize the service method in the servlet (DON'T DO THAT!).
>>From Servlet Spec 2.4, page 219:
>
[quoted text clipped - 13 lines]
> request to a
> free servlet.
No contradictions there. Juha pointed out that the programmer should not
synchronize the service method; nothing about forbidding the container to.
Juha pointed out that the container may use multiple servlet instances, same
as the servlet spec says.
A container that synchronizes access to a single instance's service() would be
at a performance disadvantage to those that use multiple instances, as "at
least Tomcat does", and likely therefore a competitive disadvantage.
- Lew