how servlet works ?

G

gk

say, i have made a servlet (say MyServlet ) and have put an entry in
the web.xml .

Now an user calls this servlet ......what happens after this ?

does an servlet instance is created by the container as soon as user
calls it ?

how those user inputs are passed to the servlet ?

say, 5 users calls the servlet at a time , does the container creates 5
instance at a time ?

what happens when the the business logic is served by the container
......does it automatically destroyed by the container ?


I know the servlet life cycle .
but how it will work in this situation is a matter of worriness.

is this flow correct ?

instance created by container when user calls ----->servlet init()
methods activates ---->then doget/dopost activates ---->process
business logic in doget/dopost ----->servlet is destroyed.


is it a correct flow ?
 
M

Manish Pandit

gk said:
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.

is this flow correct ?

instance created by container when user calls ----->servlet init()
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
 
J

Juha Laiho

Couple of corrections to what Manish wrote;

Manish Pandit said:
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!).
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.
 
M

Manish Pandit

Juha said:
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
 
L

Lew

Juha said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top