AnotherHelloWorld(Content-Length行とTelnetによる確認)

 


Content-Lengthヘッダの設定、日本語のバイト列への変換、バイナリデータの転送などに関連するサーブレット(AnotherHelloWorld)をまず体験してみよう。以下のプログラムを皆さんの開発環境に置き、またdefault_app.webappファイルに以下の部分を追加する。

 

   <servlet>

      <name>AnotherHelloWorld</name>

      <description>This is the third step of the Servlet Lesson</description>

      <code>basic_servlets.AnotherHelloWorld</code>

      <servlet-path>/</servlet-path>

      <init-parameter>

         <name></name>

         <value></value>

      </init-parameter>

      <autostart>true</autostart>

   </servlet>

 

package basic_servlets;

 

import java.lang.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

/**

 * AnotherHelloWorldは、バイト列としてHTTP応答のボディを作成するサンプルである。

 * バイナリデータの出力や、Content-Lengthヘッダ作成に有用である。

 * 作成日 : (01/05/21 13:31:36)

 * @author: Terry

 */

public class AnotherHelloWorld extends HttpServlet {

/**

 * Process incoming HTTP GET requests

 */

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    performTask(request, response);

}

/**

 * Process incoming HTTP POST requests

 */

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    performTask(request, response);

}

/**

 * 本サーブレット情報の文字列を返す。

 */

public String getServletInfo() {

    return "AnotherHelloWorld for Content-Length evaluation, Version 1.0 by Terry";

}

/**

 * AnotherHelloWorldの要求処理部では、日本語混じりのHTMLテキストをOutputStreamWriterで

 * バイトデータに変換してByteArrayOutputStreamバッファに蓄積する。このバッファのサイズから

 * Content-Lengthヘッダを作成できる。また、このバッファを使うと塊に分割した(Chunked)送信

 * を実現できる。

 */

public void performTask(HttpServletRequest request, HttpServletResponse response) {

 

    try

 

    {

        response.setContentType("text/html");

        //必要ならここに文字セット指定を以下のように追加する

//      response.setContentType("text/html; charaset=Shift_JIS");

 

        //バイト応答出力用OutputStreamの取得

        OutputStream os = response.getOutputStream();

       

        //バイト出力用ストリームを用意する

        ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024);

        //指定したサイズは、不足が生じたら自動的に拡大される           

 

        //バイト列に変換のためのOutputStreamWriterの取得

        OutputStreamWriter osw = new OutputStreamWriter(bytes, "Shift_JIS");

        System.out.println("OutputStreamWriter.getEncoding: " + osw.getEncoding());

        //エンコーディングは"Shift_JIS"や"ISO-2022-JP"や"EUC-JP"などを指定する

 

        //これを更にBufferdWriterで包んでコードコンバータがwrite()毎に呼ばれないようにする

        Writer out = new BufferedWriter(osw);

 

        //以下はHTMLの記述

        out.write("<HTML>");

        out.write("<HEAD><TITLE>Another Hello World</TITLE></HEAD>");

        out.write("<BODY><BIG>Another Hello World from クレス</BIG></BODY>");

        out.write("</HTML>");

        out.write("\r\n");

 

        //バイトアレーバッファへの移しこみ

        out.flush();

//      osw.flush();    //flushは伝播するが、明示的にしたい人はコメントを外す

        System.out.println("ByteArrayOutputStream: " + bytes.toString());

 

 

        //Content-Lengthヘッダ行の指定

        response.setContentLength(bytes.size());

 

        //バッファの送信

        bytes.writeTo(os);

        os.flush();

       

        //全てのバッファのクローズ

        out.close();

        osw.close();

        bytes.close();

        os.close();

        response.flushBuffer();

    }

    catch(UnsupportedEncodingException e){

            System.err.println("Encoding not supported: " + e.getMessage());

    }

    catch(IOException e)

    {

            System.err.println("IOException during AnotherHelloWorld.performTask: " + e.getMessage());

    }

}

}

 

Websphereテスト環境(WTE)でサーブレットエンジンを立ち上げ、このAnotherHelloWorldサーブレットがコンテナにインスタンス化されていることを先ず確認しよう。

 

 

前節     目次     次節