Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

how servlet works ?

Thread view: 
gk - 26 Oct 2006 14:21 GMT
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 ?
Manish Pandit - 26 Oct 2006 17:04 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.