Monday, December 13, 2010

Servlet

Servlet:
Servlets are protocols, platform independent server side component written in java language. Servlets can be used in any servlet enabled servers. It dynamically extend with java enabled servers.
Primitive use of servlets is to serve HTTP request-response paradigm.

As servlets run on server, they do not require GUI. Servlets are the server side counterparts of applets (Which runs on client).


Sequence of Operation:

  • Container creates a request and response object.
  • Container creates a new thread and calls the service method of the servlet with the request and response objects.
  • The service method calls the method corresponds to the HTTP request type (POST. GET).
  • Servlet send the response to the client with the response object.
  • Request and Response object loses their scope. They are destroyed or ready for garbage collection.
For each request container does not create new instance for a servlet. For each request, container assigns new thread to the same instance of the servlet.

Life Cycle of Servlet:
  • Container loads the servlet class.
  • Container creates instance of the class. Servlets no-arg constructor is being called. So constructor cannot be re-written.
  • init() method is called. This can be overridden for various parameter initialisation and connectivity ..etc.,
  • Once initialized, servlet is ready to serve (service method doPost, doGet .., etc) client request.
  • Servlet gets destroyed.
Container loads the servlet classes when the web-app is started or when the first client requests. Container gives choice to choose the mode. Even when it loads the servlet when the web-app loads, it will not service method will not run till it was initialised.

    No comments:

    Post a Comment