大学jsp实验4include,forword

、实验目的与要求

1、掌握常用JSP动作标记的使用。

二、实验内容

1、include动作标记的使用

编写一个名为shiyan4_1.jsp的JSP页面,页面内容自定,但要求使用include动作标记将在实验三中编写的includeCopyRight.jsp文件包含进该页面。

请写出相应代码:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>实验4-1</title>
 5 </head>
 6 <body>
 7 
 8 <% out.print("java代码部分"); %>
 9 <jsp:include page="../shiyan_3/includeCopyRight.jsp"/>
10 </body>
11 </html>

编写三个JSP页面:main.jsp、circle.jsp和ladder.jsp,将三个JSP页面保存在同一个WEB服务目录中。main.jsp使用include动作标记加载cricle.jsp和ladder.jsp页面。

circle.jsp页面可以计算并显示圆的面积,ladder.jsp页面可以计算并显示梯形的面积。当cricle.jsp和ladder.jsp被加载时获取main.jsp页面include动作标记的param子标记提供的圆的半径以及梯形的上底、下底和高的值。

请写出相应代码:

main.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>main</title>
 5 </head>
 6 <body>
 7 <%
 8     double r = 2.00;
 9     double up = 3.00, down = 6.00, hi = 4.00;
10 %>
11 <jsp:include page="circle.jsp">
12     <jsp:param name="banjing" value="<%=r%>"/>
13 </jsp:include>
14 <br/>
15 <jsp:include page="ladder.jsp">
16     <jsp:param name="sideup" value="<%=up%>"/>
17     <jsp:param name="sidedown" value="<%=down%>"/>
18     <jsp:param name="sidehi" value="<%=hi%>"/>
19 </jsp:include>
20 
21 </body>
22 </html>

circle.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>circle</title>
 5 </head>
 6 <body>
 7 <%!
 8     public String getCircleArea(double r){
 9         if(r>0){
10             double cArea = r*r*3.1415926;
11             return ""+cArea;
12         }
13         else{
14             return ("输入错误");
15         }
16 
17     }
18 %>
19 <%
20     String banjing = request.getParameter("banjing");
21     double r = Double.parseDouble(banjing);
22 %>
23 圆的边长为:<%=r%>,面积为:<%=getCircleArea(r)%>
24 
25 </body>
26 </html>

ladder.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>ladder</title>
 5 </head>
 6 <body>
 7 <%!
 8     public String getLadderArea(double u,double d,double h){
 9         if(u>0 && d>0 && h>0){
10             double lArea = 0.5*(u+d)*h;
11             return ""+lArea;
12         }
13         else{
14             return ("输入错误");
15         }
16 
17     }
18 %>
19 <%
20     String sideup = request.getParameter("sideup");
21     double up = Double.parseDouble(sideup);
22     String sidedown = request.getParameter("sidedown");
23     double down = Double.parseDouble(sidedown);
24     String sidehi = request.getParameter("sidehi");
25     double hi = Double.parseDouble(sidehi);
26 %>
27 梯形的上下低为:<%=up%>,<%=down%>,高为:<%=hi%>,面积为:<%=getLadderArea(up,down,hi)%>
28 
29 </body>
30 </html>

 

2、forword动作标记的使用

编写shiyan4_2.jsp、page1.jsp、page2.jsp和page3.jsp四个JSP文件,当请求shiyan4_2.jsp页面后,该页面会产生一个0~3(不包含3)的随机整数,若这个随机数是0,则将请求转发给page1.jsp页面;若这个随机数是1,则将请求转发给page2.jsp页面;若这个随机数是2,则将请求转发给page3.jsp页面。page1.jsp、page2.jsp和page3.jsp三个页面只显示“这是第n个随机页”(n=1或2或3)一句话。请写出相应代码:

 shiyan4_2.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>实验4</title>
 5 </head>
 6 <body>
 7 <%
 8     int n;
 9     int number = (int) (Math.random() * 3);
10     if (number == 0) {
11         n = number + 1;
12 %>
13 <jsp:forward page="page1.jsp">
14     <jsp:param name="page1" value="<%=n%>"/>
15 </jsp:forward>
16 <%
17     }
18     if (number == 1) {
19         n = number + 1;
20 %>
21 <jsp:forward page="page2.jsp">
22     <jsp:param name="page2" value="<%=n%>"/>
23 </jsp:forward>
24 <%
25     }
26     if (number == 2) {
27         n = number + 1;
28 %>
29 <jsp:forward page="page3.jsp">
30     <jsp:param name="page3" value="<%=n%>"/>
31 </jsp:forward>
32 <%
33     }
34 %>
35 
36 </body>
37 </html>

 

page1.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>page1</title>
 5 </head>
 6 <body>
 7 
 8 这是第<%=request.getParameter("page1")%>个页面的随机页
 9 
10 </body>
11 </html>

 

page2.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>page2</title>
 5 </head>
 6 <body>
 7 
 8 这是第<%=request.getParameter("page2")%>个页面的随机页
 9 
10 </body>
11 </html>

 

page3.jsp:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>page3</title>
 5 </head>
 6 <body>
 7 
 8 这是第<%=request.getParameter("page3")%>个页面的随机页
 9 
10 </body>
11 </html>

 

posted @ 2018-05-03 22:34  龙谷情Sinoam  阅读(1023)  评论(0编辑  收藏  举报
Smiley face