今回は、「シンプルなサーブレットを作る―サーブレットの作り方(2)―」です。
 ブラウザからパラメータを受け取るメソッドを詳しく見てみましょう。
■動画はこちら
サーブレットのコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51  | import java.io.IOException; import java.util.Map; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; public class App1 extends HttpServlet {     public App1() {         super();     }     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         //パラメータ取得         Map<String,String[]> map = request.getParameterMap();         //HTMLを作成する処理         String html = "<html>"                     + "<body>";         for(String key : map.keySet()) {             html += key + ":";             String[] vals = map.get(key);             for(String s : vals) {                 html += s + ",";             }             html +=  "<br />";         }         html +=  "</body>"                     + "</html>";         //レスポンスのヘッダーに、MIMEタイプ、文字コードをセット         response.setContentType("text/html");         response.setCharacterEncoding("UTF-8");         //作成したHTMLを出力         response.getWriter().append(html);     }     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         doGet(request,response);     } }  | 














