Java Webノート–サーブレット技術ライフサイクルコアAPIクラスメソッド呼び出し順序の紹介

6639 ワード

1、サーブレット技術:
サーブレットは、プラットフォームに関係のないサーバ側コンポーネントであり、再サーブレットコンテナで動作します.サーブレットコンテナは、サーブレットとクライアントの通信とサーブレット方式の呼び出しを担当します.サーブレットとクライアントは、「要求/応答」モードを通過します.
2、サーブレットの機能:
既存のHTMLページに埋め込み可能なHTMLコードクリップの一部を作成し、顧客要求に基づくHTMLページに戻し、他のサーバリソースと通信する
3、サーブレットフレームに関わる二つのJARパッケージ:
JAvax.servletパッケージ:すべてのサーブレットクラスが実装または拡張する必要がある汎用インタフェースとクラスを定義します.JAvax.servlet.httpパケット:HTTPプロトコル通信を用いたHttpServeretクラスを定義します.
4、サーブレットの関連クラスとサーブレットのライフサイクル:
サーブレットフレームワークの核心はJavax.servlet.サーブレットインタフェースであり、すべてのサーブレットはこのインタフェースで定義された5つの方法を実現しなければならない.
4.1、サーブレットのライフサイクルを表す3つの方法:
Initメソッドinitメソッド:サーブレットサービスの初期化メソッド:顧客要求に応答するdestroyメソッド:サーブレットオブジェクトがライフサイクルを終了したときに、使用するリソースを解放する
4.1.1、サーブレットコンテナがサーブレットを作成するタイミング:
①サーブレットコンテナ起動時にサーブレットを自動マウントする
デフォルトでは、Webクライアントがサーブレットへのアクセスを最初に要求したときに、このサーブレットのインスタンスが作成されます.要素のサブ要素が設定されている場合、サーブレットコンテナは、Webアプリケーションを開始すると、指定された順序でこのサーブレットを作成して初期化します.

      LoginServlet
      class>com.itzhai.login.LoginServletclass>
      2
  

このservletでは、グローバルな初期化情報を記述できます.
②サーブレットコンテナ起動後、お客様がサーブレットに初めて要求を送信
③サーブレット類ファイルが更新された後、サーブレットを再マウントする
サーブレットがマウントされると、サーブレットコンテナはサーブレットインスタンスを作成し、サーブレットのinit()メソッドを呼び出して初期化します.サーブレットのライフサイクル全体で、initメソッドは1回のみ呼び出されます.
4.1.2、サーブレットの応答要求段階:
サーブレットコンテナに到着した顧客要求に対して、サーブレットコンテナは、この要求に固有のサーブレットRequestオブジェクトとサーブレットResponseオブジェクトを作成し、サービスメソッドを呼び出して処理します.サービスメソッドは、サーブレットRequestオブジェクトから顧客要求情報を取得し、要求を処理し、サーブレットResponseオブジェクトを介してルートクライアント応答結果を返す.
4.1.3、サーブレットの終了段階:
Webアプリケーションが終了したり、サーブレットコンテナが実行を終了したり、サーブレットコンテナがサーブレットの新しいインスタンスを再ロードしたりすると、サーブレットコンテナはサーブレットのdestroyメソッドを呼び出してサーブレットが占有するリソースを解放します.
5、サーブレットRequestとサーブレットResponseインタフェース
5.1、サーブレットRequestインタフェース:
サーブレットRequestインタフェースには、クライアント要求情報がカプセル化されており、クライアントが使用しているプロトコルであり、クライアントデータストリームを直接バイナリで読み出すサーブレットInputStreamも提供されています.
ServeretRequestサブクラスでは、HttpServeretRequestなどのフィーチャープロトコルに関連するデータが追加されます.
サーブレットリクエストインタフェースの主な方法:
getAttribute getContentType getInputStream getParameter getRemoteAddr getRemoteHost getRemotePort
5.2、サーブレットResponseインタフェース:
サーブレットResponseインタフェースは、サーブレットに対応する結果を返す方法を提供します.サーブレットは、返されるデータの長さとMIMEタイプを設定し、出力ストリームサーブレットOutputStreamを提供します.
ServeretResponseサブクラスでは、HttpServeretResponseなどの特性プロトコルに関連するより多くのデータが提供されます.
サーブレットResponseインタフェースの主な方法:
getOutputStream getWriter getCharacterEncoding getContentType setContentType
6、サーブレットコアAPI及びその継承関係図:
6.1、サーブレット類:
public interface Servlet
Defines methods that all servlets must implement. 
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.
This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:
1. The servlet is constructed, then initialized with the init method.
2. Any calls from clients to the service method are handled.
3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

サーブレットの関連メソッド:
service
void service(ServletRequest req,
             ServletResponse res)
             throws ServletException,
                    IOException
Called by the servlet container to allow the servlet to respond to a request. 
This method is only called after the servlet's init() method has completed successfully.

6.2、共通のサーブレットGenericServiceクラス:
public abstract class GenericServlet
extends Object
implements Servlet, ServletConfig, Serializable
Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend HttpServlet instead. 
GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be directly extended by a servlet, although it's more common to extend a protocol-specific subclass such as HttpServlet.

6.3、HttpServiceクラス:
public abstract class HttpServlet
extends GenericServlet
implements Serializable
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:
• doGet, if the servlet supports HTTP GET requests
• doPost, for HTTP POST requests
• doPut, for HTTP PUT requests
• doDelete, for HTTP DELETE requests
• init and destroy, to manage resources that are held for the life of the servlet
• getServletInfo, which the servlet uses to provide information about itself
There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).
Likewise, there's almost no reason to override the doOptions and doTrace methods.

6.4、HttpServiceletクラスの各メソッドの呼び出し順序:
まずサービスメソッドを呼び出し,要求情報に基づいて特定のgetGetやdoPostなどのメソッドを呼び出す.
サーブレットはマルチスレッドで実行されるサーバであるため、同時共有を特別に処理する必要があるすべてのリソース.
各サーブレットはサーブレットインタフェースを実装する必要があります.GenericServiceletは、プロトコル固有ではない汎用のサーブレットであり、サーブレットインタフェースを実装し、HttpServiceletはGenericServiceletを継承し、ドメインHTTPプロトコルに関連する操作を実装するので、定義したサーブレットは、HttpServiceletを直接継承するだけでよい.