Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Explaining the Servlet Life Cycle.
Previous Home Next

Explain the Servlet Life Cycle.

Servlet Life Cycle defines how it is loaded and instantiated, is initialized, handles requests from clients, and is taken out of service, while life cycle in API expressed by the init, service, and methods of the javax.servlet.Servlet interface that all servlets must implement directly or indirectly through the GenericServlet or HttpServlet abstract classes.

  1. Loaded and Instantiated :The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request. when the servlet engine is started, the servlet container loads the servlet class using normal Java class loading facilities. After loading the Servlet class, the container instantiates it for use for client request.
  2. Initialization : After the Instantiated load, the container must be initialized before it can handle request to client. The container initializes the servlet instance by calling the init method of the Servlet interface with a unique (per servlet declaration) object implementing the ServletConfig interface. Error should be occurs while the servlet is initialized which is:
    • Error Conditions on Initialization : Whenever a servlet should be initialized, the servlet instance throws an exception which is UnavailableException orServletException, because the servlet must not be part of active service and must be released by the servlet container. A new instance may be instantiated and initialized by the container after a failed initialization. and remember destroy() method is not called as it is considered unsuccessful initialization.
    • Tool Considerations : Servlet should not assumed in an active container runtime until the the init() method of the Servlet interface is called. The triggering of static initialization methods when a tool loads and introspects a Web application is to be distinguished from the calling of the init method.
  3. Request Handling : When a servlet is properly initialized, the servlet container may use to handle a client request which is represented by request object ServletRequest . The ServletResponse ( )method is provide the servlet fill out the response of all the request. While in case of HTTP request, the objects provided by the container are of types HttpServletRequest HttpServletResponse Note some issue occurs by which a servlet container may handle no requests during its lifetime like:
    • Multithreading Issues : In multithread issue a servlet container may send more than one requests( concurrent request) through the service method to the servlet.
    • Exceptions During Request Handling : A ServletException signals that some error occurred during the processing of the request and that the container should take appropriate measures to clean up the request. An UnavailableException signals that the servlet is unable to handle requests either temporarily or permanently.
    • Thread Safety Issues : Implementations of the request and response objects are not guaranteed to be thread safe. This means that they should only be used within the scope of the request handling thread.
  4. End of Service : The servlet container is not required to keep a servlet loaded for any particular period of time. whenever the servlet container determines that a servlet should be removed from service, than it calls the destroy method of the Servlet interface to allow the servlet to release all the resources that is hold and save any persistent state. The destroy method only call, when it must complete execution of any thread that are currently running in the service method of the servlet, or exceed a server-defined time limit.
Figure : Shows the Life cycle of the Servlets
Previous Home Next