2011年1月3日月曜日

102.2 サーブレットの登録 (Registering Servlets)

HttpService インターフェースを用い, javax.servlet.Servlet オブジェクトを Http サービスに登録することができる. この用途のため, HttpService インターフェースには registerServlet(String,javax.servlet.Servlet,Dictionary,HttpContext) メソッドが定義されている.

javax.servlet.Servlet objects can be registered with the Http Service by using the HttpService interface. For this purpose, the HttpService interface defines the method registerServlet(String,javax.servlet.Servlet,Dictionary,HttpContext).

例えば, Http サービスの実装がマシン www.acme.com のポート 80 で動作していて, サーブレットオブジェクトが /servlet という名前に登録されていた場合, そのサーブレットオブジェクトのサービスメソッドは, ブラウザーで次の URL が使用されたときに呼び出される.

For example, if the Http Service implementation is listening to port 80 on the machine www.acme.com and the Servlet object is registered with the name "/servlet", then the Servlet object's service method is called when the following URL is used from a web browser:

  http://www.acme.com/servlet?name=bugs

  http://www.acme.com/servlet?name=bugs

全てのサーブレットオブジェクトとリソース登録は, 同じ名前空間を共有する. 現在登録されているリソースもしくはサーブレットオブジェクトと同じ名前のもとにリソースもしくはサーブレットオブジェクトを登録しようとすると, NamespaceException が投げられる. Http サービスの名前空間の扱いについては, 36 ページの「HTTP リクエストのサーブレットおよびリソースへの関連付け」を参照のこと.

All Servlet objects and resource registration share the same name-space. If an attempt is made to register a resource or Servlet object under the same name as a currently registered resource or Servlet object, a NamespaceException is thrown. See Mapping HTTP Requests to Servlet and Resource Registrations on page 36 for more information about the handling of the Http Service name-space.

サーブレットの登録ごとに, HttpContext オブジェクトが必要となる. このオブジェクトは, リソースやメディアタイプを扱う手段や, 外部リクエストの認証を扱うメソッドを提供する. 39 ページの「認証」を参照のこと.

Each Servlet registration must be accompanied with an HttpContext object. This object provides the handling of resources, media typing, and a method to handle authentication of remote requests. See Authentication on page 39.

使い易いように, Http サービスによりデフォルトの HttpContext オブジェクトが提供され, それは createDefaultHttpContext() で取得することができる. 登録メソッドに null を渡すことでも同じ効果が得られる.

For convenience, a default HttpContext object is provided by the Http Service and can be obtained with createDefaultHttpContext(). Passing a null parameter to the registration method achieves the same effect.

サーブレットオブジェクトは ServletContext オブジェクトを要求する. このオブジェクトは, Http サービスの Java サーブレット環境にアクセスするための多くの機能を提供する. Http サービスの実装により, サーブレットオブジェクトを登録するときに使用した HttpContext オブジェクトごとに, ServletContext オブジェクトが生成される. このため, 同じ HttpContext オブジェクトを用いて登録されたサーブレットオブジェクトは, 同じ ServletContext オブジェクトを共有しなければならない.

Servlet objects require a ServletContext object. This object provides a number of functions to access the Http Service Java Servlet environment. It is created by the implementation of the Http Service for each unique HttpContext object with which a Servlet object is registered. Thus, Servlet objects registered with the same HttpContext object must also share the same ServletContext object.

サーブレットオブジェクトは, 登録されて特定の Http サービスと結合したときに, 当該 Http サービスによって初期化される. 初期化は, サーブレットオブジェクトの Servlet.init(ServletConfig) メソッドを呼ぶことにより行われる. ServletConfig パラメーターにより, サーブレットオブジェクトが登録されたときに指定された初期化パラメーターへのアクセスが提供される.

Servlet objects are initialized by the Http Service when they are registered and bound to that specific Http Service. The initialization is done by calling the Servlet object's Servlet.init(ServletConfig) method. The ServletConfig parameter provides access to the initialization parameters specified when the Servlet object was registered.

そのため, 同一のサーブレットインスタンスを再利用して他の Http サービスに登録してはならず, また, 複数の名前で登録することもできない. 登録に際しては, それぞれ独立したインスタンスが必要となる.

Therefore, the same Servlet instance must not be reused for registration with another Http Service, nor can it be registered under multiple names. Unique instances are required for each registration.

次の例は, registerServlet メソッドの使用方法を示すものである.

The following example code demonstrates the use of the registerServlet method:

    Hashtable initparams = new Hashtable();
    initparams.put("name", "value");

    Servlet myServlet = new HttpServlet() {
        String name = "<not set>";

        public void init(ServletConfig config) {
            this.name = (String)config.getInitParamter("name");
        }

        public void doGet(HttpServletRequest req, HttpServletResponse rsp)
          throws IOException {
            rsp.setContentType("text/plain");
            req.getWriter().println(this.name);
        }
    };

    getHttpService().registerServlet(
        "/servletAlias",
        myServlet,
        initparams,
        null // use default context
    );
    // myServlet が登録され, init メソッドが呼び出し済みである.
    // 外部リクエストは処理され, 当該サーブレットに転送される.
    ...
    getHttpService().unregister("/servletAlias");
    // myServlet は登録解除され, destroy メソッドが呼び出し済みである.

    Hashtable initparams = new Hashtable();
    initparams.put("name", "value");

    Servlet myServlet = new HttpServlet() {
        String name = "<not set>";

        public void init(ServletConfig config) {
            this.name = (String)config.getInitParamter("name");
        }

        public void doGet(HttpServletRequest req, HttpServletResponse rsp)
          throws IOException {
            rsp.setContentType("text/plain");
            req.getWriter().println(this.name);
        }
    };

    getHttpService().registerServlet(
        "/servletAlias",
        myServlet,
        initparams,
        null // use default context
    );
    // myServlet has been registered
    // and its init method has been called. Remote
    // requests are now handled and forwarded to
    // the servlet.
    ...
    getHttpService().unregister("/servletAlias");
    // myServlet has been unregistered and its
    // destroy method has been called

この例では, myServlet サーブレットを /servletAlias というエイリアスに登録している. 以降の http://www.acme.com/servletAlias に対するリクエストは myServlet サーブレットにマップされ, リクエストの処理のために当該サーブレットの service メソッドが呼び出される. (service メソッドは HttpServlet ベースクラスで呼ばれ, 使用されている HTTP リクエストメソッドにもとづき, doGet, doPut, doPost, doOptions, doTrace もしくは doDelete の呼び出しへとディスパッチがおこなわれる.)

This example registers the servlet, myServlet, at alias: /servletAlias. Future requests for http://www.acme.com/servletAlias maps to the servlet, myServlet, whose service method is called to process the request. (The service method is called in the HttpServlet base class and dispatched to a doGet, doPut, doPost, doOptions, doTrace or doDelete call depending on the HTTP request method used.)