4.3 Servlet Life Cycle

 < Day Day Up > 

Servlets 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.

  • If the servlet is based on the GenericServlet class, the service() method is abstract, so it must be overriden. The service() method obtains information about the client request, prepares the response, and returns this response to the client. As multiple clients might access the service() method at the same time, multiple threads will be executing the code in the single instance of the servlet. Therefore, the service() method must include any required synchronization code.

  • If the servlet is based on the HttpServlet classthe majority of the casesthe service() method is not abstract. Therefore, it can be used as is. More commonly, the servlet overrides one or more of the methods doGet() , doPut() , doPost() , and doDelete() . The default implementation of service() calls one of these methods, based on the client request. For example, if the client request is an HTTP GET method, the servlet's service() method will invoke the doGet() method; if the request is an HTTP POST method, the service() method will invoke the doPost() method, and so on.

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.

  • If the servlet is based on the GenericServlet class, the two objects are instances of the javax.servlet.ServletRequest and javax.servlet.ServletResponse interfaces, respectively.

  • If the servlet is based on the HttpServlet class, the two objects are instances of the javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse interfaces, respectively.

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 invokes methods on the request object in order to discover information about the client and server environments, as well as the parameter names and values. For example, the servlet can call methods on the request object to obtain all the data entered on a form on the client's Web browser and set by the HTTP GET and POST methods.

  • The servlet invokes methods from the response object to send the response information back to the servlet container, which then sends it to the client.

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 .

  1. The servlet is loaded. This operation is typically performed when the first client accesses the servlet. In most servlet containers, options are provided to force the loading of the servlet when the servlet container starts up.

  2. The servlet container creates an instance of the servlet.

  3. The servlet container calls the servlet's init() method. This method is called only once during the lifetime of the servlet.

  4. A client request arrives at the servlet container, and the servlet container creates a request object: ServletRequest or HttpServletRequest .

  5. The servlet container creates a response object: ServletResponse or HttpServletResponse .

  6. The servlet container invokes the servlet's service() method.

  7. The service() method takes the request object as one of its two parameters.

  8. The service() method takes the response object as the other parameter.

  9. The service() method gets information about the request object and processes the request accessing other resources, such as databases and files.

  10. The service() method retrieves the necessary information from the resources accessed.

  11. The service() method uses methods of the response object.

  12. The service() method passes the response back to the servlet container.

  13. The servlet container passes the response back to the client.

Figure 4.3. Servlet Life Cycle

graphics/04fig03.gif

For 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 servlettypically when the servlet container is shut downthe 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 > 


Enterprise Java Security. Building Secure J2EE Applications
Enterprise Javaв„ў Security: Building Secure J2EEв„ў Applications
ISBN: 0321118898
EAN: 2147483647
Year: 2004
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net