Latest

recent

The servlet life cycle


A client of a servlet-based application does not usually communicate directly with a servlet, but requests the servlet’s services through a Web server or application server that invokes the servlet through the Java Servlet API. The server’s role is to manage the loading and initialization of the servlet, the servicing of the request, and the unloading or destroying of the servlet. This is generally provided by a servlet manager function of the application server.

Typically, there is one instance of a particular servlet object at a time in the Web servers’ environment. This is the underlying principle to the persistence of the servlet. The Web server is responsible for handling the initialization of this servlet when the servlet is first loaded into the environment, where it remains active (or persistent) for the life of the servlet.

Each client request to the servlet is handled via a new thread against the original instance object. The Web server is responsible for creating the new threads to handle the requests. The Web server is also responsible for the unloading or reloading of the servlets. This might happen when the Web application is brought down, or the underlying class file for the servlet changes, depending on the underlying implementation of the server.

❑  Servlet1 is initially loaded by the Web application server. Instance variables are initialized, and remain active (persistent) for the life of the servlet.

❑  Two Web browser clients have requested the services of Servlet1. A handler thread is spawned by the server to handle each request. Each thread has access to the originally loaded instance variables that were initialized when the servlet was loaded.

❑  Each thread handles its own requests, and responses are sent back to the calling client.

 Figure given below shows a basic client-to-servlet interaction:


The life cycle of a servlet is expressed in the Java Servlet API in the init, service (doGet or doPost), and destroy methods of the Servlet interface. We will discuss the functions of these methods in more detail and the objects that they manipulate. Figure Given below is a visual diagram of the life-cycle of an individual servlet.



Understanding the life-cycle

This section describes in detail some of the important servlet life-cycle methods of the Java Servlet API.

Servlet Initialization: init method

Servlets can be dynamically loaded and instantiated when their services are first requested, or the Web server can be configured so that specific servlets are loaded and instantiated when the Web server initializes.

In either case, the init method of the servlet performs any necessary servlet initialization, and is guaranteed to be called once for each servlet instance, before any requests to the servlet are handled. An example of a task which may be performed in the init method is the loading of default data parameters or database connections.

The most common form of the init method of the servlet accepts a ServletConfig object parameter. This interface object allows the servlet to access name/value pairs of initialization parameters that are specific to that servlet. The ServletConfig object also gives us access to the SevletContext object that describes information about our servlet environment. Each of these objects will be discussed in more detail in the servlet examples sections.

Servlet request handling

Once the servlet has been properly initialized, it may handle requests (although it is possible that a loaded servlet may get no requests). Each request is represented by a ServletRequest object, and the corresponding response by a ServletResponse object in the Java Servlet API. Since we will be dealing with HttpServlets, we will deal exclusively with the more specialized HttpServletRequest and HttpServletResponse objects.

The HttpServletRequest object encapsulates information about the client request, including information about the client’s environment and any data that may have been sent from the client to the servlet. The

HttpServletRequest class contains methods for extracting this information from the request object.
The HttpServletResponse is often the dynamically generated response, for instance, an HTML page which is sent back to the client. It is often built with data from the HttpServletRequest object. In addition to an HTML page, a response object may also be an HTTP error response, or a redirection to another URL, servlet, or JavaServer Page. 

Each time a client request is made, a new servlet thread is spawned which services the request. In this way, the server can handle multiple concurrent requests to the same servlet. For each request, usually the service, doGet, or doPost methods will be called. These methods are passed the HttpServletRequest and HttpServletResponse parameter objects.

doPost: Invoked whenever an HTTP POST request is issued through an HTML form. The parameters associated with the POST request are communicated from the browser to the server as a separate HTTP request. The doPost method should be used whenever modifications on the server will take place.

doGet: Invoked whenever an HTTP GET method from a URL request is issued, or an HTML form. An HTTP GET method is the default when a URL is specified in a Web browser. In contrast to the doPost method, doGet should be used when no modifications will be made on the server, or when the parameters are not sensitive data. The parameters associated with a GET request are appended to the end of the URL, and are passed into the QueryString property of the HttpServletRequest.

Other servlet methods worth mentioning

destroy: The destroy method is called when the Web server unloads the servlet. A subclass of HttpServlet only needs to implement this method if it needs to perform cleanup operations, such as releasing database connections or closing files.

getServletConfig: The getServletConfig method returns a ServletConfig instance that can be used to return the initialization parameters and the ServletContext object.

getServletInfo: The getServletInfo method is a method that can provide information about the servlet, such as its author, version, and copyright. This method is generally overwritten to have it return a meaningful value for your application. By default, it returns an empty string.
The servlet life cycle Reviewed by 1000sourcecodes on 23:29 Rating: 5
Powered by Blogger.