■JSP サンプル■1行だけのJava
選択したラジオボタンに対応したメッセージを表示するJSPです。
<%@ page contentType="text/html; charset=Shift_JIS" %> <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title> サンプル1:送信パラメータ受け取り(チェックボックス) </title> </head> <body bgcolor="#aaddaa"> <form method="get" action="JSP_SMPL01.jsp"> 選択1:<input type="radio" name="Radio1" value="一番目" /><br /> 選択2:<input type="radio" name="Radio1" value="二番目" /><br /> 選択3:<input type="radio" name="Radio1" value="三番目" /> <input type="submit" name="SUB1" value="送信" /> <hr /> 選択したのは <%= request.getParameter("Radio1") %> です。 <br /> </form> </body> </html>
■条件分岐
パラメータ有無を判定して、初期表示と応答表示を分けるよう、条件分岐を入れたJSPです。
<%@ page contentType="text/html; charset=Shift_JIS" session = "false" %> <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title> サンプル2:初期状態チェック </title> </head> <body bgcolor="#aaddaa"> <form method="post" action="JSP_SMPL02.jsp"> 入力1: <% if (request.getParameter("Text1") == null) { // パラメータ無し(初期表示)のとき %> <input type="text" name="Text1" size="10" /> <% } else { // パラメータあり(応答表示)のとき %> <input type="text" name="Text1" size="10" value="<%= request.getParameter("Text1") %>" /> <% } %> <input type="submit" name="SUB1" value="送信" /> <hr /> <% if (request.getParameter("Text1") != null) { // パラメータあり(応答表示)のとき結果表示 %> 受けたデータは <%= request.getParameter("Text1") %> です。 <br /> <% } %> </form> </body> </html>
■多重分岐
多重分岐を使用したJSPです。 switch ではなく else if を使用しています。
<%@ page contentType="text/html; charset=Shift_JIS" session = "false" %> <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title> サンプル3:多重分岐 </title> </head> <body bgcolor="#aaddaa"> <form method="get" action="JSP_SMPL03.jsp"> 入力1: <% String w_msg1 = null; // テキストボックス未入力時のメッセージ String w_msg2 = null; // 色未選択時のメッセージ w_msg1 = ""; w_msg2 = ""; if (request.getParameter("SUB1") == null) { /* パラメータ無し(初期表示)のとき 送信ボタンにnameを付けているので、ボタンが押されて いればパラメータにあるはず */ %> <input type="text" name="Text1" size="20" /> <hr /> <input type="radio" name="SelColor" value="teal" />teal<br /> <input type="radio" name="SelColor" value="aqua" />aqua<br /> <input type="radio" name="SelColor" value="lime" />lime <% } else { // パラメータあり(応答表示)のとき if ((request.getParameter("Text1") == null) || (request.getParameter("Text1").equals(""))) { // パラメータ無しのとき w_msg1 = "文字が入力されていません"; %> <input type="text" name="Text1" size="20" /> <hr /> <% } else { %> <input type="text" name="Text1" size="20" value="<%= request.getParameter("Text1") %>" /> <hr /> <% } if ((request.getParameter("SelColor") == null) || (request.getParameter("SelColor").equals(""))) { // パラメータ無しのとき w_msg2 = "色が選ばれてません"; %> <input type="radio" name="SelColor" value="teal" />teal<br /> <input type="radio" name="SelColor" value="aqua" />aqua<br /> <input type="radio" name="SelColor" value="lime" />lime <% } else if (request.getParameter("SelColor").equals("teal")) { %> <input type="radio" name="SelColor" value="teal" checked />teal<br /> <input type="radio" name="SelColor" value="aqua" />aqua<br /> <input type="radio" name="SelColor" value="lime" />lime <% } else if (request.getParameter("SelColor").equals("aqua")) { %> <input type="radio" name="SelColor" value="teal" />teal<br /> <input type="radio" name="SelColor" value="aqua" checked />aqua<br /> <input type="radio" name="SelColor" value="lime" />lime <% } else { %> <input type="radio" name="SelColor" value="teal" />teal<br /> <input type="radio" name="SelColor" value="aqua" />aqua<br /> <input type="radio" name="SelColor" value="lime" checked />lime <% } } %> <input type="submit" name="SUB1" value="送信" /> <br /> <hr /> <br /><%= w_msg1 %> <%= w_msg2 %> <br /> <% if ((request.getParameter("SUB1") != null) && (w_msg1.equals("")) && (w_msg2.equals(""))) { // 初期表示以外でエラーメッセージもないとき %> <table border="1" width="200"><tr><td bgcolor="<%= request.getParameter("SelColor") %>"> <%= request.getParameter("Text1") %></td></tr></table> <% } else { %> <table border="1" width="200"><tr><td> </td></tr></table> <br /> <% } %> </form> </body> </html>
■正規表現パターンチェック用JSP
本文中にも出てますが、サンプルページにも載せておきます。切り貼りするときは reg_chk.jsp というファイル名にしてください。
<%@ page contentType="text/html; charset=Shift_JIS" session = "false" import = "java.util.regex.*" %> <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title> 正規表現マッチングテスト </title> </head> <body bgcolor="#aaddaa"> <form method="get" action="reg_chk.jsp"> <h3> 正規表現マッチングテスト </H3> <% String w_src_data = null; String w_chk_ptn = null; w_src_data = request.getParameter("src_data"); w_chk_ptn = request.getParameter("chk_ptn"); if ((w_src_data == null) || (w_chk_ptn == null)) { %> <p> 検査文字列 : <input type="text" name="src_data" size="50"> <br /> 正規表現パターン : <input type="text" name="chk_ptn" size="30"> <br /> </p> <input type="submit" value="送信"> <br /><hr /><br /> <% } else { %> <p> 検査文字列 : <input type="text" name="src_data" size="50" value=<%= w_src_data %> > <br /> 正規表現パターン : <input type="text" name="chk_ptn" size="30" value=<%= w_chk_ptn %> > <br /> </p> <input type="submit" value="送信"> <br /><hr /><br /> <% if (w_chk_ptn.equals("")) { out.print("正規表現パターンがありません <br />"); } else { if (Pattern.matches(w_chk_ptn, w_src_data)) { out.print("パターンにマッチしました<br />"); } else { out.print("<font color=\"red\">パターンにマッチしません<br />"); out.print("</font>"); } } } %> <br /> </body> </html>
■データチェックと繰り返し
画面を表示しようとするのが
<%@ page contentType="text/html; charset=Shift_JIS" session = "false" %> <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title> サンプル4:繰り返し処理 </title> </head> <body bgcolor="#aaddaa"> <form method="get" action="JSP_SMPL04.jsp"> 背景色 : <% String w_msg = null; // エラーメッセージ boolean w_sw_err = false; // エラースイッチ int w_no_in = 0; // 入力した数字 int w_no_sel = 0; // 選択したリンクの位置 int w_lp_cnt = 0; // ループカウンタ String w_HTML_Tbl = ""; // table表示用HTML String w_HTML_Link = ""; // link表示用HTML w_sw_err = false; w_msg = ""; if ((request.getParameter("SUB1") == null) && (request.getParameter("Sel_Item") == null)) { /* * パラメータ無し(初期表示)のとき * 送信ボタンにnameを付けているので、ボタンが押されて * いればパラメータにあるはず * またリンクをクリックしたときのパラメータも無ければ初期表示と見なせる */ %> 数字を指定してください: <input type="text" name="Text1" size="3" maxlength="2" /> <input type="submit" name="SUB1" value="送信" /> <hr /> <% } else { /* 送信ボタンが押されたかリンクがクリックされたとき */ %> 数字を指定してください: <input type="text" name="Text1" size="3" maxlength="2" value="<%= request.getParameter("Text1") %>" /> <input type="submit" name="SUB1" value="送信" /> <hr /> <% if (request.getParameter("Sel_Item") == null) { /* 送信ボタンが押されたとき * リンクをクリックしたときのパラメータが無ければ送信ボタン押下と見なせる */ try { w_no_in = Integer.parseInt(request.getParameter("Text1")); if ((w_no_in <= 0) || (w_no_in > 30)) { w_sw_err = true; w_msg = "数字は1〜30の整数で指定してください"; } } catch (NumberFormatException WL_excp) { w_sw_err = true; w_msg = "数字を入力してください"; } if (!(w_sw_err)) { w_HTML_Tbl = ""; w_HTML_Link = ""; for (w_lp_cnt = 1; w_lp_cnt <= w_no_in; w_lp_cnt = w_lp_cnt + 1) { w_HTML_Tbl = w_HTML_Tbl + "<tr><td width=\"200\"> </td></tr>"; // テーブルの全行数もパラメータとして渡るように、リンクに設定する w_HTML_Link = w_HTML_Link + "<tr><td width=\"200\"><a href=\"JSP_SMPL04.jsp?Sel_Item=" + w_lp_cnt + "&Text1=" + w_no_in + "\">" + w_lp_cnt + "番目</a></td></tr>"; } } } else { /* リンクがクリックされたとき */ w_no_in = Integer.parseInt(request.getParameter("Text1")); w_no_sel = Integer.parseInt(request.getParameter("Sel_Item")); w_HTML_Tbl = ""; w_HTML_Link = ""; for (w_lp_cnt = 1; w_lp_cnt <= w_no_in; w_lp_cnt = w_lp_cnt + 1) { if (w_lp_cnt == w_no_sel) { // リンクで選ばれた位置 w_HTML_Tbl = w_HTML_Tbl + "<tr><td width=\"200\" bgcolor=\"aqua\"> </td></tr>"; } else { w_HTML_Tbl = w_HTML_Tbl + "<tr><td width=\"200\"> </td></tr>"; } w_HTML_Link = w_HTML_Link + "<tr><td width=\"200\"><a href=\"JSP_SMPL04.jsp?Sel_Item=" + w_lp_cnt + "\">" + w_lp_cnt + "番目</a></td></tr>"; } } // 左のテーブルと右のリンク表示域を分けるため、 // レイアウト用にテーブルを使用する out.println("<br />" + w_msg); if (!(w_sw_err)) { out.println("<table><tr><td width=\"300\">"); out.println("<table border=\"1\">"); out.println(w_HTML_Tbl); out.println("</table>"); out.println("</td><td width=\"300\">"); out.println("<table border=\"0\">"); out.println(w_HTML_Link); out.println("</table>"); out.println("</td></tr></table>"); } } %> <br /> </form> </body> </html>
■色指定チェック用JSP
これも一応サンプルとして載せておきます。テキストボックスに maxlength= を指定しているので2文字以上は入力できないはずですが、それでも文字数チェックはしておきましょう。それと2文字必須入力にしてあります。例えば入力が"a"のとき、"0a"に拡張するようなことはしていません。
<%@ page contentType="text/html; charset=Shift_JIS" session = "false" import = "java.util.regex.*" %> <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title> 色指定チェック用JSP </title> </head> <body bgcolor="#aaddaa"> <form method="get" action="clr_chk.jsp"> 背景色 : <% String w_msg = null; // エラーメッセージ boolean w_sw_err = false; // エラースイッチ String w_BG_RR = null; // 背景色(赤成分) String w_BG_GG = null; // 背景色(緑成分) String w_BG_BB = null; // 背景色(青成分) String w_Font_RR = null; // 文字色(赤成分) String w_Font_GG = null; // 文字色(緑成分) String w_Font_BB = null; // 文字色(青成分) w_sw_err = false; w_msg = ""; if (request.getParameter("SUB1") == null) { /* パラメータ無し(初期表示)のとき 送信ボタンにnameを付けているので、ボタンが押されて いればパラメータにあるはず */ %> RR=<input type="text" name="BG_RR" size="2" maxlength="2" value="" /> GG=<input type="text" name="BG_GG" size="2" maxlength="2" value="" /> BB=<input type="text" name="BG_BB" size="2" maxlength="2" value="" /> <hr /> 文字色 : RR=<input type="text" name="FONT_RR" size="2" maxlength="2" value="" /> GG=<input type="text" name="FONT_GG" size="2" maxlength="2" value="" /> BB=<input type="text" name="FONT_BB" size="2" maxlength="2" value="" /> <% } else { // パラメータあり(応答表示)のとき w_BG_RR = request.getParameter("BG_RR"); w_BG_GG = request.getParameter("BG_GG"); w_BG_BB = request.getParameter("BG_BB"); w_Font_RR = request.getParameter("FONT_RR"); w_Font_GG = request.getParameter("FONT_GG"); w_Font_BB = request.getParameter("FONT_BB"); %> RR=<input type="text" name="BG_RR" size="2" maxlength="2" value="<%= w_BG_RR %>" /> GG=<input type="text" name="BG_GG" size="2" maxlength="2" value="<%= w_BG_GG %>" /> BB=<input type="text" name="BG_BB" size="2" maxlength="2" value="<%= w_BG_BB %>" /> <hr /> 文字色 : RR=<input type="text" name="FONT_RR" size="2" maxlength="2" value="<%= w_Font_RR %>" /> GG=<input type="text" name="FONT_GG" size="2" maxlength="2" value="<%= w_Font_GG %>" /> BB=<input type="text" name="FONT_BB" size="2" maxlength="2" value="<%= w_Font_BB %>" /> <% // 背景色のチェック if ((w_BG_RR == null) || (w_BG_RR.equals(""))) { w_sw_err = true; w_msg = "背景色:赤 が入力されていません"; } else { // 16進数は必ず2桁指定を前提にしている // (Integer.parseInt(w_BG_RR, 16)を使ってもいいんだけど、 // 別途文字数チェックが必要なので matches を使った) if (!(Pattern.matches("[\\da-fA-F][\\da-fA-F]", w_BG_RR))) { w_sw_err = true; w_msg = "背景色:赤 の指定が16進文字ではありません"; } } if (!(w_sw_err)) { if ((w_BG_GG == null) || (w_BG_GG.equals(""))) { w_sw_err = true; w_msg = "背景色:緑 が入力されていません"; } else { if (!(Pattern.matches("[\\da-fA-F][\\da-fA-F]", w_BG_GG))) { w_sw_err = true; w_msg = "背景色:緑 の指定が16進文字ではありません"; } } } if (!(w_sw_err)) { if ((w_BG_BB == null) || (w_BG_BB.equals(""))) { w_sw_err = true; w_msg = "背景色:青 が入力されていません"; } else { if (!(Pattern.matches("[\\da-fA-F][\\da-fA-F]", w_BG_BB))) { w_sw_err = true; w_msg = "背景色:青 の指定が16進文字ではありません"; } } } // 以下、文字色のチェック if (!(w_sw_err)) { if ((w_Font_RR == null) || (w_Font_RR.equals(""))) { w_sw_err = true; w_msg = "文字色:赤 が入力されていません"; } else { if (!(Pattern.matches("[\\da-fA-F][\\da-fA-F]", w_Font_RR))) { w_sw_err = true; w_msg = "文字色:赤 の指定が16進文字ではありません"; } } } if (!(w_sw_err)) { if ((w_Font_GG == null) || (w_Font_GG.equals(""))) { w_sw_err = true; w_msg = "文字色:緑 が入力されていません"; } else { if (!(Pattern.matches("[\\da-fA-F][\\da-fA-F]", w_Font_GG))) { w_sw_err = true; w_msg = "文字色:緑 の指定が16進文字ではありません"; } } } if (!(w_sw_err)) { if ((w_Font_BB == null) || (w_Font_BB.equals(""))) { w_sw_err = true; w_msg = "文字色:青 が入力されていません"; } else { if (!(Pattern.matches("[\\da-fA-F][\\da-fA-F]", w_Font_BB))) { w_sw_err = true; w_msg = "文字色:青 の指定が16進文字ではありません"; } } } } %> <br /> <hr /> <input type="submit" name="SUB1" value="送信" /> <br /> <hr /> <br /><%= w_msg %> <br /> <% if ((request.getParameter("SUB1") == null) || (w_sw_err)) { // 初期表示かエラーがあるとき %> <table border="1" width="200"><tr><td>文字の色</td></tr></table> <% } else { %> <table border="1" width="200"><tr><td bgcolor="#<%= w_BG_RR + w_BG_GG + w_BG_BB %>"> <font color="#<%= w_Font_RR + w_Font_GG + w_Font_BB %>">文字の色</font></td></tr></table> <% } %> <br /> </form> </body> </html>
|