2019-11-1 题目:编写三个JSP页面:main.jsp, circle.jsp, ladder.jsp, 将3个页面保存在同一个Web服务目录中。main.jsp使用include动作标记加载circle.jsp,ladder.jsp页面。circle.jsp页面可以计算并显示圆面积,ladder.jsp页面可以计算并显示梯形面积。当circle.jsp、ladder.jsp被加载时获取main.jsp页面中include动作标记的param子标记提供的圆半径及梯形的上底、下底、高的值。
main.jsp代码
<%@ page contentType="text/html;charset=GB2312" %> <!-- jsp指令标记 --> <html> <body>main页面<br> <form action=""> 梯形上底:<input type = "text" name="up"><br> 梯形下底:<input type = "text" name="down"><br> 梯形的高:<input type = "text" name="high"><br> <input type = "submit" value = "计算梯形面积" name = "submit1"><br> <% String a_top = request.getParameter("up"); String a_bottom = request.getParameter("down"); String a_high = request.getParameter("high"); if(a_top == null || a_bottom == null || a_high == null){ //刚加载页面时未点击提交按钮,即未创建对象 out.println("未获取梯形的上底、下底、高"); } else if("".equals(a_top) || "".equals(a_bottom) || "".equals(a_high)){ //输入值为空的情况 out.println("请输入梯形的上底、下底、高"); } else { %> 链接到ladder.jsp页面计算梯形面积<br> <jsp:include page="ladder.jsp"> <jsp:param value="<%= a_top %>" name="top"/> <jsp:param value="<%= a_bottom %>" name="bottom"/> <jsp:param value="<%= a_high %>" name="high"/> </jsp:include> <% } %> </form> <form action=""> 圆的半径:<input type = "text" name="r"><br> <input type = "submit" value = "计算圆面积" name = "submit1"><br> <% String r = request.getParameter("r"); if(r == null){ out.println("未获取圆的高"); } else if("".equals(r)){ out.println("请输入圆的高"); } else { %> 链接到circle.jsp页面计算梯形面积<br> <jsp:include page="circle.jsp"> <jsp:param value="<%= r %>" name="radius"/> </jsp:include> <% } %> </form> </body> </html>circle.jsp代码
<%@ page contentType="text/html;charset=GB2312" %> <!-- jsp指令标记 --> <html> <body>round<br> <% String r = request.getParameter("radius"); double pi = 3.14; double R = Double.parseDouble(r); double area = pi*R*R; out.print("radius=" + r + ", pi=" + pi + "<br>" + "area=" + area); %> </body> </html>ladder.jsp代码
<%@ page contentType="text/html;charset=GB2312" %> <!-- jsp指令标记 --> <html> <body>trapezoidal<br> <% String a_top = request.getParameter("top"); String a_bottom = request.getParameter("bottom"); String a_high = request.getParameter("high"); double top = Double.parseDouble(a_top); double bottom = Double.parseDouble(a_bottom); double high = Double.parseDouble(a_high); out.print("top:" + top + "<br>" + "bottom:" + bottom + "<br>" + "high:" + a_high + "<br>"); double s = (top+bottom)*(high/2); out.print("area=" + s); %> </body> </html>运行结果