< Day Day Up > |
4.3 Servlet Life CycleServlets use packages found in the Java Servlet API. When you write code for a Java servlet, you must use at least one of the following two packages: javax.servlet for any type of servlet and/or javax.servlet.http for servlets specific to HTTP. Servlets are usually created by extending from the javax.servlet.http.HttpServlet abstract class, which in turn extends the javax.servlet.GenericServlet abstract class, or from the GenericServlet class itself, which implements the javax.servlet.Servlet interface. Both the GenericServlet and the HttpServlet classes contain three methods that they inherit from the Servlet interface: init(), service(), and destroy(). These methods, used by the servlet to communicate with the servlet container, are called life-cycle methods. You work with these three methods in a slightly different way, depending on whether you are extending the GenericServlet class or the HttpServlet class. The init() and the destroy() methods have the same properties for the GenericServlet and the HttpServlet classes; the service() method must be handled differently when it is based on the GenericServlet class or on the HttpServlet class. The init() method is run only once after the servlet container loads the servlet class and the servlet is instantiated, regardless of how many clients access the servlet. This method is guaranteed to finish before any service() requests are processed. The servlet can be activated when the servlet container starts or when the first client accesses the servlet. A custom init() method is typically used to perform setup of servlet-wide resources only once rather than once per request, as in the service() method. For example, a customized init() can load Graphics Interchange Format (GIF) images once, whereas the servlet service() method returns the images multiple times in response to multiple client requests to the servlet. Further examples include initializing sessions with other network services or establishing access to persistent data, such as databases and files. The destroy() method is run only once when the servlet container stops the servlet and unloads it. Usually, servlets are unloaded when the servlet container is shut down. The default destroy() method can be accepted as is, without the need to override it, because it is not abstract. Servlet writers may, if they wish, override the destroy() call, providing their own custom destroy() method. A custom destroy() method is often used to manage servletwide resources. For example, the destroy() method can be used to release shared servletwide resources allocated in the init() method. The service() method is the heart of a servlet. The simplest servlets define only the service() method. Unlike the init() and destroy() methods, service() is called for each client request. The service() method must be handled differently when it is based on the GenericServlet class or on the HttpServlet class.
It is through the service() method that the servlet and the servlet container exchange data. When it invokes the servlet's service() method, the servlet container also passes in two objects as parameters. These objects are known as the request and response objects, and their implementation depends on the servlet's superclass.
The request and response objects encapsulate the data sent by the client, providing access to parameters and allowing the servlets to report status information, including any errors that occurred. The servlet container creates an instance for the request and response objects and passes them to the servlet. Both of these objects are used by the servlet container to exchange data with the servlet.
The servlet life cycle involves a series of interactions among the client, the servlet container, and the servlet, as shown in Figure 4.3. The steps are as follows.
Figure 4.3. Servlet Life CycleFor additional client requests, the servlet container creates new request and response objects, invokes the service() method of the servlet, and passes the request and response objects as parameters. This loop is repeated for every client request but without the need to call the init() method every time. When the servlet container no longer needs the servlet�typically when the servlet container is shut down�the servlet container invokes the servlet destroy() method. As discussed earlier, a servlet based on the HttpServlet class generally overrides one or more of the HTTP methods doGet(), doPut(), doPost(), and doDelete(), and the default implementation of the service() method calls one of these methods, based on the client request. In this case, the servlet life cycle just described should take into account the additional call that the service() method makes to the HTTP method. |
< Day Day Up > |
No comments:
Post a Comment