Friday, November 27, 2009

Servlet Container & Request processing

Why we need Container for Servlets?

    Servlets don't have main() method,They run under
    control of another java application called container.
    Ex: Tomcat

When Server gets request for servlet the server handovers
the request to container in which Servlet is deployed.

In simple "Container manages and runs the Servlet".

Advantages of using Container:

Communication
The container provides easy way of communication between servlets and web server.

Life-cycle Management:
The container controls the life cycle of servlet.
    Loading classes.
    Instantiating and initializing the servlet.
    Invoking servet methods ie  like init service & destroy .
    Destroying servlets for garbage collection ( Resource management ).

Threading support:
Container automatically creates a new java thread for every
request it receives.The thread dies when servlet completes
running the HTTP service method.
  
Security:   
    Container provides XML based configuration for security configuration and
modification of servlet with out changing the java code of servlet.
  
JSP support:
    Container takes care of converting JSP code into java.      

Handling the request:

When user request for Dynamic content
1)Container gets request  for servlet.
2)Container creates HTTPServletResponse and HTTPResponse objects.
3)Based on the requested URL Container maps the request to a  sevlet
and creates a thread for the request  and  passes the references of
HTTPServletResponse and HTTPResponse objects.
4)Container calls the service(),Which in-turn calls the doXXX()
methods based on the request type.
5)the doXXX() method constructs the dynamic page into HTTPResponse object.
6)When response is sent to client container deletes the objects and thread.

No comments: