Adv Java [Servlet: Life Cycle]

♠ Posted by Unknown in at 07:05

Servlets


Client access from the Internet or corporate intranets is a sure way to allow many users to access data and resources easily. This type of access is based on clients using the World Wide Web standards of Hypertext Markup Language (HTML) and Hyper Text Transfer Protocol (HTTP). The Servlet API set abstracts a common solution framework for responding to HTTP requests.


Servlet provide an excellent solution for server-side programming support, they are one of the most popular reasons for moving to Java. Not only do they provide a framework that replaces CGI programming (and eliminates a number of thorny CGI problems), but all your code has the platform portability gained from using Java, and you have access to all the Java API (except, of course, the ones that produce GUIs, like swing).

The architecture of the servlet API is that of a classic service provider with a service() method through which all client requests will be sent by the servlet container software, and life cycle methods init() and destroy() which are called only when the servlet is loaded and unloaded (this happens rarely).

Servlet Life Cycle


Image: Servlet Life Cycle


The Servlet interface methods relevant to the servlet lifecycle are init(), service(), and destroy(). The life cycle starts with the container calling the init() method, and ends with the container calling the destroy() method.

The lifecycle of a servlet consists of the following fundamental stages:
  1. Instantiation :- the web container creates an instance of the servlet
  2. Initialization :- The container calls the instance’s init{} method.
  3. Service :- If the container has a request for the servlet, it calls the servlets instance’s service() method.
  4. Destroy :- Before destroying the instance, the container calls the servlet instance’s detroy() method.
  5. Unavailable :- The instance is destroyed and marked for garbage collection.

0 comments:

Post a Comment