SelectMessage.java



/**
 * 選択された番号に対応するメッセージを返すサービス
 */

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SelectMessage extends HttpServlet {

  /** 選択された番号を記憶 */
    public int selectedNum=0;

  /** 選択された番号を記憶 */
    public static String[] messages = {
        "Error:",
        "I love an elephant very much.........",
        "Wakkanai is the coolest city in Java.",
        "Welcome to Wakkanai Hokusei..........",
    };

    public void doGet( HttpServletRequest request,
                       HttpServletResponse response )
                       throws IOException, ServletException
    {
        InputStream in = request.getInputStream();

        try {
            int i = in.read();
            selectedNum
              = Integer.parseInt( String.valueOf( (char)i ) );
        }
        catch( IOException e ) {
            selectedNum = 0;
        }
        catch( NumberFormatException e ) {
            selectedNum = 0;
        }

        response.setContentType("text/plain");

        PrintWriter out = response.getWriter();
        out.println( messages[ selectedNum ] );
    }

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