基本サーブレットをプロジェクトに追加する

このステップでは、プロパティでアスタリスクが設定されている JSP フォームをロードする実行可能なサーブレット・プログラムを作成します。

  1. CobolBookReader プロジェクトの src ディレクトリを右クリックし、[New > Other] を選択します。
  2. [Micro Focus COBOL] を展開して [COBOL JVM Class] を選択し、[Next] をクリックします。
  3. クラス名を BookServlet として入力し、[Finish] をクリックしてクラスを作成します。
  4. 次の指令をファイルの先頭に追加します。
           $set ilusing"javax.servlet.http"
           $set ilusing"javax.servlet"
           $set ilnamespace"com.microfocus.book"
           $set sourceformat"variable"
    これにより、Java サーブレット API のネームスペースがインポートされ、クラスのネームスペースが com.microfocus.book に設定されます。また、80 以上の列を使用できるようにソースのフォーマットが設定されます。
  5. class-id エントリを次のように編集します。
    class-id BookServlet public inherits HttpServlet.
    これにより、BookServlet は HttpServlet 基本クラスから継承するようになるため、Tomcat は BookServlet をサーブレットとしてロードすることができます。
  6. instanceMethod メソッド・テンプレートを削除します。
  7. 次の項目を working-storage section ヘッダの下に追加します。
           78 stockNoAttribute     value "stockno".
           78 titleAttribute       value "title".
           78 authorAttribute      value "author".
           78 typeAttribute        value "type".
           78 priceAttribute       value "price".
           78 onHandAttribute      value "onhand".
           78 soldAttribute        value "sold".
           78 stockValAttribute    value "stockval".
    これらの属性文字列の値はサーブレットの転送中に JSP に組み込まれます。
  8. 次のメソッドを 78 項目の下に追加します。
           method-id doProcessing protected.
           01 viewUrl      string value "/BookJsp.jsp".
           01 dispatcher   type RequestDispatcher.
           procedure division using by value req as type HttpServletRequest
                                             res as type HttpServletResponse
                                             isGet as condition-value.       
               invoke req::setAttribute(stockNoAttribute   "****")
               invoke req::setAttribute(titleAttribute     "*************************************")
               invoke req::setAttribute(authorAttribute    "*************************************")
               invoke req::setAttribute(typeAttribute      "****")
               invoke req::setAttribute(priceAttribute     "****")
               invoke req::setAttribute(onHandAttribute    "****")
               invoke req::setAttribute(soldAttribute      "****")
               invoke req::setAttribute(stockValAttribute  "****")
    
               set dispatcher to self::getServletContext()::getRequestDispatcher(viewUrl)
               invoke dispatcher::forward(req res)
               goback.
           end method.
    setAttribute() メソッドを使用して、サーブレット要求属性にアスタリスクを渡します。呼び出しが JSP に転送されると、これらの属性がフォームに表示されます。これは、JSP のディスパッチャを取得してから、要求および応答オブジェクトを使用してそのディスパッチャ上で呼び出し転送することによって行われます。このメソッドは、post HTTP 呼び出しと get HTTP 呼び出しの両方を処理するために使用されます。
  9. 次のメソッドをプログラムに追加します。
           method-id doGet protected override.
           procedure division using by value req as type HttpServletRequest
                                             res as type HttpServletResponse.
               invoke self::doProcessing(req res true)
           
               goback.
           end method.
           method-id doPost protected override.
           procedure division using by value req as type HttpServletRequest
                                             res as type HttpServletResponse.
               invoke self::doProcessing(req res false)
           
               goback.
           end method.
    これらのメソッドは、HttpServlet クラス内の同等のメソッドをオーバーライドし、このサーブレットが post 要求または get 要求を行うように求められるたびに呼び出されます。