ここでは、JSTLを使った条件分岐の方法を学びます。
以下のプログラムを作成しなさい。
(チームで、ホワイトボードを使い、方針を決めてからプログラミングを始めること)
なお、乱数を発生させるには、Math.random()を使うこと。
例えば、次のようにすれば0から100までの整数値が取得できる。
int score = (int) (Math.random() * 101);
1.<c:if>の基本
テストの点数が80点以上かどうかを判定したい。
以下のQ01サーブレットで0から100までの乱数を発生させ、その値をリクエストスコープに入れてJSPにフォワードし、80点以上なら「合格です」と表示しなさい。80点未満なら何も表示しないこと。
package p21; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "Q01", urlPatterns = {"/p21/Q01"}) public class Q01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int score = (int)(Math.random() * 101); req.setAttribute("score", score); req.getRequestDispatcher("q01.jsp").forward(req, resp); } }
解答例を見る
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>q01</title> </head> <body> <c:if test="${score>=80}"> <p>点数:${score}<br>合格です</p> </c:if> </body> </html>
2. <c:if> で複数条件を判定
以下のQ02サーブレットで0から100までの乱数を発生させ、その値をリクエストスコープに入れてJSPにフォワードし、10点以上90点未満なら「平凡な点数です」と表示しなさい。条件に当てはまらない場合は何も表示しないこと。
package p21; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "Q02", urlPatterns = {"/p21/Q02"}) public class Q02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int score = (int)(Math.random() * 101); req.setAttribute("score", score); req.getRequestDispatcher("q02.jsp").forward(req, resp); } }
解答例を見る
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>q02</title> </head> <body> <c:if test="${score>=10 and score<90}"> <p>点数:${score}<br>平凡な点数です</p> </c:if> </body> </html>
3. <c:if> で空文字またはnullを判定
以下のQ03サーブレットで文字列を設定し、その値をリクエストスコープに入れてJSPにフォワードし、文字列がnullまたは空文字の場合は「 nullまたは空文字です」と表示させなさい。
package p21; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "Q03", urlPatterns = {"/p21/Q03"}) public class Q03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String string = ""; req.setAttribute("string", string); req.getRequestDispatcher("q03.jsp").forward(req, resp); } }
解答例を見る
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>q03</title> </head> <body> <c:if test="${empty string}"> <p> nullまたは空文字です</p> </c:if> </body> </html>
オリジナル問題作成
将来の後輩のために良い問題が出来たら教えてください。
以上。