how servlet works ?

Discussion in 'Java' started by gk, Oct 26, 2006.

  1. gk

    gk Guest

    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 ?
    gk, Oct 26, 2006
    #1
    1. Advertising

  2. gk wrote:

    >
    > 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
    Manish Pandit, Oct 26, 2006
    #2
    1. Advertising

  3. gk

    Juha Laiho Guest

    Couple of corrections to what Manish wrote;

    "Manish Pandit" <> said:
    >gk wrote:
    >> 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.
    --
    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)
    Juha Laiho, Nov 8, 2006
    #3
  4. Juha Laiho wrote:
    > 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
    Manish Pandit, Nov 8, 2006
    #4
  5. gk

    Lew Guest

    > Juha Laiho wrote:
    >> 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!).


    Manish Pandit wrote:
    >>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.


    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
    Lew, Nov 12, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    2,392
    Andy Fish
    Dec 17, 2003
  2. Sean Clarke
    Replies:
    1
    Views:
    1,872
    Sudsy
    Jan 7, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    1,967
    Jack Jia
    Apr 4, 2004
  4. chamikara
    Replies:
    1
    Views:
    49,817
  5. DiscoStu
    Replies:
    0
    Views:
    488
    DiscoStu
    Jul 8, 2004
Loading...

Share This Page