Servlet Life Cycle

As stated earlier, servlets are deployed within a servlet container, which in turn is hosted by a Web server. The particular capabilities and level of compliance of the Web server determines which version of the servlet specification you need to be working with.

The basic behavior of a servlet involves a request-response type model derived from the way the HTTP works; thus, the inherent applicability as a Web component. This behavior is illustrated via a statechart diagram in Figure 10-1.

Figure 10-1. Servlet life cycle


Servlets are built as Java classes that extend one of two basic servlet implementation classes: HttpServlet and GenericServlet. The former is the most often used, yet slightly more complex of the two. Both servlet types employ the same basic life cycle.

Life Cycle Methods

The servlet life cycle makes use of three basic request handler methods, of which any or all can be implemented within the extended servlet class:

  • init: Initializes the servlet

  • service: Services the client request

  • destroy: Destroys the servlet

Of these three methods, the service method is the most interesting because it actually does the majority of the necessary processing. It typically does the following:

  • Receives the request from the client

  • Reads the request data

  • Writes the response headers

  • Gets the writer or output stream object for the response

  • Writes the response data

The service method is at the heart of the GenericServlet type. However, it is almost never overridden and instead is split into lower level HTTP request handlers when used with the HttpServlet type.

The init and destroy life cycle methods are always available to be over ridden, but in several cases might not be used if the servlet has no specific objects or connections it needs to initialize or terminate.

A sequence diagram in Figure 10-2 shows a simple example of a servlet. This diagram applies to both the GenericServlet and HttpServlet. It highlights a simple example where a database query is made to formulate the response to the client. Note that the service method is further refined into a specific HTTP request in the case of HttpServlet.

Figure 10-2. Sequence diagram showing servlet life cycle


Convenience Method

Besides the life cycle methods, servlets commonly make use of what are referred to as convenience methods. One such convenience method that applies for all servlets is getServletInfo, which returns a general info string about the particular servlet—normally author, version, usage, and so on.

Required Methods and Tagged Values

When building a servlet that extends the GenericServlet class, the service life cycle method must be implemented; otherwise, the servlet is invalid. All other methods are optional.

Multiple threads may call a generic servlet instance's service method concurrently. To avoid this, the servlet can implement the SingleThreadModel interface, which is really a method of typing the servlet and indicating to the Web container that only a single thread should be allowed to call the method at any given time.

Implementing the SingleThreadModel can have a very significant effect on how the container decides to allocate resources when the servlet is deployed on the Web server, which can greatly impact the total number of concurrent servlet instances allowed.

Using this approach may be appropriate if you are dealing with a situation in which the servlet may need to alter information that is not thread safe or access resources that are not thread safe.

It is not recommended that you attempt to serialize any of the servlet methods other than by implementing this interface. The interface itself introduces no new methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.157