Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
198
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Servlets and JSPs

Packages

javax.servlet, javax.servlet.http, javax.servlet.jsp, javax. servlet.jsp.taglib

Use: J2EE (J2EE1.2)

Overview

The servlet API provides one of the two Web component technologies for J2EE. The general model for the servlet API is quite straightforward, based on two packages which hold all of the core functionality:

javax.servlet – Provides the generic servlet model

javax.servlet.http – Adapts the servlet model to HTTP and HTTPS

The base architectural model for servlets makes no assumptions for the request-response protocol apart from the fact that it runs on a TCP/IP backbone. Predictably, the resources which are available to a servlet built on this model are a bit basic, consisting of only those things that would be available from a bare-bones set of assumptions about the communication channel.

For all practical purposes, the majority of servlet work is based on the functionality from the javax.servlet.http package. This adapts the core servlet architecture to the Web world, providing a model which assumes either HTTP or HTTPS as the underlying protocol.

Main API Elements

Three API elements provide the structural foundation for servlets: the Servlet interface, the GenericServlet class and the HttpServlet class.

The Servlet interface defines, among other things, the methods of a servlet's lifecycle: init, destroy and

service.

The GenericServlet is an abstract class that provides a basic implementation of the interface, with the exception of the service method. Since any servlet will at least have to specify some behavior in response to a client request, this method is left unimplemented for the Generic-Servlet.

The HttpServlet is also an abstract class, but all of its methods are implemented. This class provides a service method that branches into seven doXxx methods, which correspond to most of the modern HTTP requests: doGet,

doPost, doPut, doHead, doOptions, doTrace and doDelete.

Life Cycle

As a component, the servlet's lifecycle depends on management by a Web container. The container orchestrates calls to a number of methods in the servlet API. The following list shows the major servlet lifecycle methods and the order in which they're called:

Create one or more servlet objects

Initialize the servlet through calls to init

Process client requests by using worker threads to run a servlet's service methods, which branch to a series of doXxx methods that match basic HTTP commands (doGet, doPut, doPost, and so on)

Call the destroy method

Destroy the servlet objects

JavaServer Pages

213